Return to site

☕🪆 SOLID PRINCIPLES: the LISKOV SUBSTITUTION PRINCIPLE 🔃

If a subtype surprises its parent’s clients, inheritance failed.

🔸 TL;DR

The L in SOLID stands for the Liskov Substitution Principle.

Derived classes must be substitutable 🔃 for their base classes without changing the expected behavior of the program.

This past Saturday was Barbara Liskov's birthday.

A good opportunity to remember not only the principle carrying her name, but the person behind it.

Section image

🔸 FROM FORTRAN TO THE "L" IN SOLID

In 1961, Barbara Liskov applied to Princeton for graduate studies.

She received a response saying the university did not admit women to its graduate program.

She moved to Boston instead and joined MITRE.

On her first day as a programmer, she was given a FORTRAN manual and a problem to solve.

She essentially taught herself programming.

Decades later, her work on:

▪️ Data abstraction

▪️ Programming languages

▪️ Polymorphism

▪️ Distributed systems

▪️ Behavioral subtyping

would help shape modern Software Engineering.

And yes: the L in SOLID bears her name.

🔸 WHAT DOES LSP ACTUALLY SAY?

In simple terms:

If class A extends class B, code using B should also work correctly when B is replaced by A. 🔃

Inheritance 🪆 is not simply:

"A Square IS-A Rectangle, so Square extends Rectangle."

The real question is:

Can a Square behave everywhere a Rectangle is expected?

Consider the classic Java example:

Rectangle shape = new Square();

shape.setWidth(5);
shape.setHeight(3);

System.out.println(shape.area());

A Rectangle client expects:

5 × 3 = 15

A Rectangle client expects:But if Square forces width and height to remain equal, setHeight(3) may also change the width.

@Override
public void setHeight(double h) {
    super.setHeight(h);
    super.setWidth(h);
}

Result:

3 × 3 = 9

💥 The subtype changed the expected behavior.

That's an LSP violation.

💡A client using Rectangle expects setHeight() to modify only the height, but the Square implementation also modifies the width.

The mathematical statement:

Square IS-A Rectangle

doesn't automatically mean that:

class Square extends Rectangle

is a good software design.

With this mutable API, it isn't.

💡The distinction is important:

Mathematically: A square IS-A rectangle because it has four right angles.

Structurally: Square extends Rectangle is valid Java inheritance.

Behaviorally: Square violates LSP because it cannot preserve the independent width and height modifications expected from Rectangle.

🔸 THINK IN CONTRACTS, NOT JUST HIERARCHIES

A superclass defines a contract.

A subtype should preserve that contract.

That means, among other things:

▪️ Don't strengthen preconditions

▪️ Don't weaken postconditions

▪️ Preserve the parent's invariants

▪️ Don't introduce unexpected exceptions

▪️ Keep return values compatible with what clients expect

Concept Meaning Example
Invariant Must remain true throughout an object's valid states balance >= 0
Precondition Must be true before a method executes amount > 0
Postcondition Must be true after a method executes Balance decreases by amount

The question isn't:

"Can I technically extend this class?"

It's:

"Will callers notice if I substitute the subtype?"

If the answer is yes ⚠️, your abstraction probably needs another look.

🔸 3️⃣ LSP WARNING SIGNS

Some of my favorite signals of a questionable hierarchy:

▪️ Lots of instanceof checks

if (shape instanceof Square) {
    ...
}

▪️ Subclasses implementing methods that do nothing

▪️ Subclasses throwing UnsupportedOperationException for behavior promised by the parent

Those often mean the abstraction is lying.

🔸 A BETTER DIRECTION

Instead of forcing Square into Rectangle's mutable contract:

sealed interface Shape
        permits Rectangle, Square {

    double area();
}

Then let each type respect its own invariants.

record Rectangle(
    double width,
    double height
) implements Shape {

    public double area() {
        return width * height;
    }
}
record Square(
    double side
) implements Shape {

    public double area() {
        return side * side;
    }
}

Now both are Shapes.

Neither has to pretend to support behavior that doesn't make sense.

🔸 WHY LSP STILL MATTERS

SOLID is sometimes presented as a collection of abstract rules.

But LSP addresses a very practical problem:

surprises.

Good abstractions allow us to replace implementations without rewriting all the code around them.

That's fundamental to:

▪️ Maintainability 🛠️

▪️ Loose coupling 🔗

▪️ Polymorphism 🔃

▪️ Testability 🧪

▪️ Extensible architectures 🧱

Barbara Liskov's work went far beyond one letter in an acronym.

Her research helped establish foundations for data abstraction, programming languages and reliable distributed systems: contributions that ultimately earned her the ACM A.M. Turing Award.

Not bad for someone who was once told:

"We don't admit women."

🔸 TAKEAWAYS

▪️ Inheritance is about behavior, not just taxonomy.

▪️ A subtype must preserve the expectations established by its supertype.

▪️ If replacing the parent with the child breaks client code, you've violated LSP.

▪️ instanceof, unsupported operations and special-case logic can reveal broken abstractions.

▪️ Sometimes the best inheritance hierarchy is the one you decide not to create.

Happy belated birthday, Barbara Liskov. 🎂

And thank you for the L. 🪆🔃

#Java #SOLID #LiskovSubstitutionPrinciple #LSP #SoftwareEngineering #SoftwareArchitecture #ObjectOrientedProgramming #OOP #CleanCode #Programming #BarbaraLiskov #TuringAward

Go further with Java certification:

Java👇

Spring👇

SpringBook👇

JavaFullstackBook👇