ArticleslgStudy

science

Liskov substitution principle

Liskov substitution principle is a science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Liskov substitution principle rather than just read about it. In short: The Liskov substitution principle (LSP) is a particular definition of a subtyping relation, called strong behavioral subtyping, that was initially introduced by Barbara Liskov in a 1987 conference keynote address titled Data abstraction and hierarchy. It is based on the concept of "substitutability" – a principle in object-oriented programming stating that an object of a superclass may be replaced by an object of a…

Liskov substitution principle — main illustration
Liskov substitution principle — illustration

Key takeaways

  • Liskov substitution principle belongs to science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Liskov substitution principle to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Liskov substitution principle from memory before moving on to harder problems.

Reference excerpt

The Liskov substitution principle (LSP) is a particular definition of a subtyping relation, called strong behavioral subtyping, that was initially introduced by Barbara Liskov in a 1987 conference keynote address titled Data abstraction and hierarchy. It is based on the concept of "substitutability" – a principle in object-oriented programming stating that an object of a superclass may be replaced by an object of a subclass without breaking the program. It is a semantic rather than merely syntactic relation, because it intends to guarantee semantic interoperability of types in a hierarchy, object types in particular. Barbara Liskov and Jeannette Wing described the principle succinctly in a 1994 paper as follows:

Subtype Requirement: Let ⁠ ϕ ( x ) {\displaystyle \phi (x)} ⁠ be a property provable about objects ⁠ x {\displaystyle x} ⁠ of type T. Then ⁠ ϕ ( y ) {\displaystyle \phi (y)} ⁠ should be true for objects ⁠ y {\displaystyle y} ⁠ of type S where S is a subtype of T. Symbolically:

S ≤ T → ( ∀ x : T . ϕ ( x ) → ∀ y : S . ϕ ( y ) ) {\displaystyle {\texttt {S}}\leq {\texttt {T}}\to (\forall x{:}{\texttt {T}}.\phi (x)\to \forall y{:}{\texttt {S}}.\phi (y))}

That is, if S subtypes T, what holds for T-objects holds for S-objects. In the same paper, Liskov and Wing detailed their notion of behavioral subtyping in an extension of Hoare logic, which bears a certain resemblance to Bertrand Meyer's design by contract in that it considers the interaction of subtyping with preconditions, postconditions and invariants.

Principle Liskov's notion of a behavioural subtype defines a notion of substitutability for objects; that is, if S is a subtype of T, then objects of type T in a program may be replaced with objects of type S without altering any of the desirable properties of that program (e.g., correctness). Behavioural subtyping is a stronger notion than typical subtyping of functions defined in type theory, which relies only on the contravariance of parameter types and covariance of the return type. Behavioural subtyping is undecidable in general: if q is the property "method for x always terminates", then it is impossible for a program (e.g., a compiler) to verify that it holds true for some subtype S of T, even if q does hold for T. Nonetheless, the principle is useful in reasoning about the design of class hierarchy. Liskov substitution principle imposes some standard requirements on signatures that have been adopted in newer object-oriented programming languages (usually at the level of classes rather than types; see nominal vs. structural subtyping for the distinction):

Contravariance of method parameter types in the subtype. Covariance of method return types in the subtype. New exceptions cannot be thrown by the methods in the subtype, except if they are subtypes of exceptions thrown by the methods of the supertype. Along with signature requirements, the subtype must meet several behavioural conditions. These are detailed in a terminology resembling that of design by contract methodology, leading to some restrictions on how contracts can interact with inheritance:

Preconditions cannot be strengthened in the subtype. Postconditions cannot be weakened in the subtype. Invariants cannot be weakened in the subtype. History constraint (the "history rule"). Objects are regarded as being modifiable only through their methods (encapsulation). Because subtypes may introduce methods that are not present in the supertype, the introduction of these methods may allow state changes in the subtype that are not permissible in the supertype. The history constraint prohibits this. It was the novel element introduced by Liskov and Wing. A violation of this constraint is, for example, defining a mutable point as a subtype of an immutable point. This is a violation of the history constraint, because in the history of the immutable point, the state is always the same after creation, so it cannot include the history of a mutable point in general. Fields added to the subtype may, however, be safely modified because they are not observable through the supertype methods. Thus, one can define a circle with immutable center and mutable radius as a subtype of an immutable point without violating the history constraint.

Origins The rules on pre- and postconditions are identical to those introduced by Bertrand Meyer in his 1988 book Object-Oriented Software Construction. Both Meyer, and later Pierre America, who was the first to use the term behavioral subtyping, gave proof-theoretic definitions of some behavioral subtyping notions, but their definitions did not take into account aliasing that may occur in programming languages that support references or pointers. Taking aliasing into account was the major improvement made by Liskov and Wing (1994), and a key ingredient is the history constraint. Under the definitions of Meyer and America, a mutable point would be a behavioral subtype of an immutable point, whereas Liskov substitution principle forbids this.

Violation Liskov substitution principle explains a property, "If for each object o1 of type S there is an object o2 of type T such that for all programs P defined in terms of T, the behavior of P is unchanged when o1 is substituted for o2 then S is a subtype of T,". Here is an example of violation of LSP:

From a programming point of view, the Square class may be defined as extending the Rectangle class.

However, this violates LSP even though the is-a relationship holds between Rectangle and Square. Consider the following example, where function g does not work if a Square is passed in, and so the open-closed principle might be considered to have been violated.

… excerpt ends here. Continue reading the full article.

Illustrations

Liskov substitution principle: Liskov substitution was introduced by Barbara Liskov.
Liskov substitution was introduced by Barbara Liskov.

Worked examples

Example 1 — a first encounter with Liskov substitution principle

Start with the simplest possible case. Write down what Liskov substitution principle claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Liskov substitution principle before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Liskov substitution principle ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Liskov substitution principle

In research
Liskov substitution principle appears in science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Liskov substitution principle in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Liskov substitution principle is common in secondary-school and first-year university syllabi. It links to neighbouring topics Formal methods, Object-oriented programming, Programming language semantics, so understanding it makes those chapters shorter.
In everyday life
Look for Liskov substitution principle outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Liskov substitution principle” →

Affiliate

Preply — study more efficiently by working with a personal tutor. 50% off.

How to study Liskov substitution principle in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Liskov substitution principle means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Liskov substitution principle out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Liskov substitution principle in simple terms?

The Liskov substitution principle (LSP) is a particular definition of a subtyping relation, called strong behavioral subtyping, that was initially introduced by Barbara Liskov in a 1987 conference keynote address titled Data abstraction and hierarchy. It is based on the concept of "substitutability…

Why does Liskov substitution principle matter?

Because it connects several science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Liskov substitution principle?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Liskov substitution principle.

Tags

  • Formal methods
  • Object-oriented programming
  • Programming language semantics
  • Programming principles
  • Type theory

Keep exploring