2.2.6 · D1Design Principles

Foundations — SOLID — Liskov Substitution Principle

1,335 words6 min readBack to topic

This page assumes you know nothing. Before you can read SOLID — Liskov Substitution Principle, you must be able to read six little pieces of vocabulary. We build each one from a plain-words meaning, anchor it to a picture, and say exactly why the topic needs it.


0. The scene: a caller, a base, a subclass

Everything in LSP is a three-way story between:

  • a base class — a general kind of thing (e.g. Bird),
  • a subclass — a more specific version of it (e.g. Penguin),
  • a caller — some other code that was written to use the base, and never heard of the subclass.

Why the topic needs these: LSP is entirely about whether a subclass's methods behave like the base's methods. No classes, no methods, no LSP.


1. Inheritance and the extends arrow

Why the topic needs it: LSP is a rule about what inheritance is allowed to do. Without inheritance there is no subclass to substitute.


2. Overriding — replacing a method

Why the topic needs it: every LSP violation on the parent page is an override that misbehaves — Square.setWidth, Penguin.fly. The danger lives in the override.


3. Polymorphism — one call, many bodies

Why this tool and not if-checks? The whole payoff of inheritance is writing code once and trusting all subtypes. If you had to write if (bird is Penguin) ... everywhere, you'd gain nothing. LSP is precisely the condition under which polymorphism is safe. See Polymorphism.


4. The contract: preconditions, postconditions, invariants

A contract is the deal a method offers its caller: "if you give me valid inputs, I promise a valid result." Three words describe it.

Why the topic needs them: the parent's three rules are entirely stated in these words. This whole vocabulary comes from Design by Contract. LSP = "a subclass must not break the base's contract."


5. Reading the symbols , , , and the arrow

Now Liskov's formal sentence becomes readable.

So Liskov's line —

reads in plain words: "Any true fact about base objects must stay true for subclass objects." That's the one-sentence idea, dressed in symbols.

Let and be the contract pieces. The parent's two arrows now decode cleanly:


Prerequisite map

Class and Object

Method

Inheritance extends

Override

Polymorphism

Contract pre post invariant

Symbols T S phi implies

Liskov Substitution Principle

Related principles that lean on the same foundations: SOLID — Open Closed Principle (needs polymorphism), SOLID — Interface Segregation Principle (fixes fat contracts), SOLID — Dependency Inversion Principle, SOLID — Single Responsibility Principle.


Equipment checklist

What is a class
A blueprint bundling data (fields) with behaviour (methods); each stamped copy is an object.
What is a method
A named action a class can perform.
What does extends / inheritance give a subclass
All the base's fields and methods, with the option to override some.
Overriding vs overloading
Overriding = same name AND same inputs, replacing the base version; overloading = same name but different inputs.
What is polymorphism
One call written against the base runs whichever subclass's body is actually present at runtime.
What is a precondition
What must be true before a call — the caller's duty.
What is a postcondition
What the method promises after it returns — the method's duty.
What is an invariant
A fact that stays true the whole life of the object.
What does mean
is a subtype of : every can stand in wherever a was expected.
What does mean
A property (true fact) about object .
What does mean, and why one-directional here
"Implies"; the subclass need not equal the base, only keep every base-truth, so the arrow only points base to sub.