🔸 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.
🔸 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:
A Rectangle client expects:
A Rectangle client expects:But if Square forces width and height to remain equal, setHeight(3) may also change the width.
Result:
💥 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:
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
The question isn't:
It's:
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
▪️ 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:
Then let each type respect its own invariants.
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:
🔸 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👇