SOLIDL2 · Easy
LSP: The Square–Rectangle Problem
Problem
The classic LSP violation: Square extends Rectangle. Since a square's width and height must always be equal, setting width changes height too — this breaks the Rectangle contract and surprises callers. Identify WHY this violates LSP, then redesign the shape hierarchy correctly.
Requirements
- Explain the violation: show a test that passes for Rectangle but fails for Square
- Fix Option A: make both Shape subclasses without inheritance between them
- Fix Option B: use a read-only Shape interface (area(), perimeter()) — no setters
- Show that the fixed design passes the same test for both Square and Rectangle
- Apply the same thinking to Bird → FlyingBird → Penguin (another LSP violation)
Constraints
- –Square must NOT extend Rectangle (or vice versa) in the final design
- –Substituting Square for Shape in any caller must not break the caller
✓ Saved