Vol. I a living index of named laws · est. mmxxvi no ads · no account
The Law Tomeindex of named laws

The Liskov Substitution Principle

also known as — LSP, Behavioural subtyping, The substitution property
"An object of a superclass must be replaceable by an object of any subclass without breaking the program."
In plain English

What does The Liskov Substitution Principle mean?

Inheritance is a promise, not a convenience. If code works with a type, it must go on working when handed any subtype of it — otherwise every caller has to know which subtype it really has, and the abstraction has bought nothing.

Reliability

Rated Heuristic: a dependable rule of thumb, not a proven theorem. The other 224.

Lineage
1987COINED1996POPULAR
How it works

How does The Liskov Substitution Principle work?

Liskov and Wing's formal version states it as a property of provable statements: if φ(x) holds of objects x of type T, then φ(y) must hold of objects y of type S wherever S is a subtype of T. Practically, that constrains a subtype in ways a compiler will not: it may not demand more of its callers than the parent did, may not deliver less than the parent promised, may not throw exceptions the parent did not, and may not break the invariants the parent maintained. A subtype that violates any of these still compiles, because the type system checks signatures and not behaviour, which is why the principle has to be stated at all.

Where you'll see it

What are examples of The Liskov Substitution Principle?

Square and Rectangle The standard case. A Square is a Rectangle in geometry, but a Rectangle whose width and height can be set independently is not substitutable by a Square, which must change both. Code that widens a rectangle and checks the height is unchanged fails on the subtype.
The subclass that refuses A read-only collection inherits from a mutable one and throws on every write. Every caller that took a collection must now ask what kind it is before writing: the substitution has failed, and the type has stopped carrying information.
Strengthened preconditions A parent accepts any string; the override rejects empty ones. Nothing in the signature says so, so the failure appears at a call site written years earlier by someone who read the parent's contract.
Why it matters

Why does The Liskov Substitution Principle matter?

It is the reason 'is-a' is the wrong test for inheritance and 'behaves-as' is the right one, and it supplies the L in the SOLID set that has shaped two decades of object-oriented design teaching. More broadly it is the clearest statement of a general truth about interfaces: a type system checks that the shapes match, and nothing checks that the promises do.

Working with it

How do you apply The Liskov Substitution Principle?

  • Test the parent's contract against the child — run the superclass's test suite over every subclass. A subtype that fails a parent's test is a substitution violation, stated as a red test rather than as an argument.
  • Prefer composition when the promise does not hold — if the subtype genuinely cannot honour the parent's contract, the relationship was never inheritance. Hold the object instead of extending it.
  • Weaken preconditions, strengthen postconditions — the safe direction. A subtype may accept more and promise more; it may never accept less or promise less.
Where it breaks down

What are the limits of The Liskov Substitution Principle?

It is a constraint on behavioural subtyping, and behaviour is not fully specified by most codebases: applying it requires knowing what the parent actually promised, which is often nowhere written down. It says nothing about whether an inheritance hierarchy is a good idea in the first place, only about what a hierarchy must satisfy once it exists. The formal version is also stronger than most designs need or can check: Liskov and Wing's condition quantifies over provable properties, and no mainstream language enforces it.

What it doesn't say

What are common misconceptions about The Liskov Substitution Principle?

It is not the rule that a subclass must not override anything; overriding is exactly what it governs. Nor is it a claim that Square should never inherit from Rectangle in any design: the violation comes from the mutable setters, and an immutable Rectangle has no such problem. It is also not a compiler feature: nothing in a type system stops you from breaking it.

Is it real?

Is The Liskov Substitution Principle real?

Real as a rule of thumb, not as a theorem. The Liskov Substitution Principle is rated Heuristic here: dependable enough to plan with, and with no proof behind it.

Rated from three sources. 225 of the 1,116 entries carry this rating. How the scale works.

Occasionally printedin printed books: one of 240 names in its band, of 903 measured
25 of 69entries in software & systems carry the same rating

The best-known laws are the least likely to rest on measurement; the full verdict on The Liskov Substitution Principle.

Origin

Where did The Liskov Substitution Principle come from?

Introduced by Barbara Liskov in a 1987 conference keynote, "Data Abstraction and Hierarchy", and given a rigorous form by Liskov with Jeannette Wing in "A Behavioral Notion of Subtyping" (ACM Transactions on Programming Languages and Systems, 1994). The name and the practitioner-facing statement come from Robert C. Martin, whose 1996 Engineering Notebook column in the C++ Report put it as: functions that use pointers or references to base classes must be able to use objects of derived classes without knowing it. It later became the L of Martin's SOLID set.

How the name spread

When did people start saying “The Liskov Substitution Principle”?

1800coined 19872019
How often “Liskov Substitution Principle” appears in printed English, which takes hold around 1995 and peaks around 2019. Coined 1987 and in print by about 1995, unusually close together. This still charts the phrase rather than the idea. Source: Google Books Ngrams.

What we checked

  • 1,116entries in the tomeListed one by one on the browse page.
  • 1,116of them citing a sourceOpen any entry: its sources sit at the foot of the page.
  • 2,489source references in allEach is named, and linked wherever the source is online.