Stage 2 · Lesson 3 of 7
Liskov substitution
The says a subtype must work anywhere its parent is expected, without surprising the caller. A subtype may not demand more from callers, promise less in return, or throw where the parent would not. The classic example is Square extending Rectangle.
Code that sets width to 5 and height to 4 expects an area of 20, but a Square keeps its sides equal, so the area comes out as 16. Mathematically a square is a rectangle; as objects with setters, it is not a safe substitute. When substitution breaks, callers start adding type checks and falls apart.
Common trapoverriding a method just to throw "not supported", which is the clearest sign the subtype was never a true .
A question to check yourself, an AI practice session and 1 cheat sheet entry come with the full roadmap. Unlock
Resources for this stage
Start with· follow one of these
- CoursePaidGrokking SOLID Design Principles
Design Gurus · Beginner · $50 one-time
One chapter per principle, each with the violation and the refactoring that fixes it
- CourseFreeMIT 6.102: Software Construction
MIT · Intermediate
Free: the Specifications and Interfaces and Subtyping readings explain substitution as contracts