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).

This is a Pro question

Sign in, then upgrade to Pro or Power to unlock this question and the full Interview Prep bank.

What is the Liskov Substitution Principle, and what's a classic example of violating it?

Next Step

Continue to What is the Abstract Factory pattern? Give a real-world Java example.← Back to all Design Patterns & LLD questions