Skip to main content
Practice Problems

What is Liskov Substitution Principle (LSP)?

javascript
// Bad: Square breaks Rectangle behavior class Rectangle { setWidth(w) { this.width = w; } setHeight(h) { this.height = h; } area() { return this.width * this.height; } } class Square extends Rectangle { setWidth(w) { this.width = w; this.height = w; // Changes both! } } // Breaks LSP: function test(rectangle) { rectangle.setWidth(5); rectangle.setHeight(4); console.log(rectangle.area()); // Expected: 20 } test(new Rectangle()); // 20 ✅ test(new Square()); // 16 ❌ Broken!

Short Answer

Interview ready
Premium

A concise answer to help you respond confidently on this topic during an interview.

Finished reading?
Practice Problems