intermediateSOLID Principles
What is the Liskov Substitution Principle, and what's a classic example of violating it?
Subtypes must be substitutable for their base type without altering the correctness of the program — anywhere a Base object is expected, a Derived object should work without surprising behavior. The classic violation is Square extending Rectangle: if setWidth() and setHeight() on Rectangle are independent, but Square must keep both equal, then a Square silently breaks any code that relies on Rectangle's contract (e.g. setting width to 5 and height to 10 and expecting area == 50).
Ready to master this question?
Generate a complete walkthrough — background, the full answer in plain language, a working code example explained line by line, a real-world scenario, common mistakes, and how this same question gets asked in different ways.
Sign in to generate a response