Skip to content
Veltoryx Technologies
ProLesson 3.3Intermediate7 min readUpdated 31 October 2026

Liskov Substitution Principle

Learn why a child class must work everywhere its parent is expected, see the famous Rectangle and Square trap, and learn the contract rules that keep inheritance safe.

Imagine you book a "standard car" from a rental company. When you arrive, they hand you the keys to a vehicle that looks like a car, has four wheels and a steering wheel, but the brake pedal does nothing. Technically it is a car, because the label says so. But you were promised something, and this vehicle does not keep the promise. You would be right to complain.

The third SOLID principle is about exactly this kind of promise in code. It is named after the computer scientist Barbara Liskov, and it says:

If a piece of code works with a parent class, it must also work with any child class of it, without surprises.

In other words, a child must be substitutable for its parent. If you replace the parent object with a child object, the program must still behave correctly. Inheritance is only safe when this is true.

01A classic trap: the Rectangle and the Square

In mathematics, every square is a rectangle. So it feels natural to write the code like this:

A Square that extends a Rectangle
public class Rectangle {
    protected int width;
    protected int height;

    public void setWidth(int width)   { this.width = width; }
    public void setHeight(int height) { this.height = height; }
    public int area()                 { return width * height; }
}

public class Square extends Rectangle {

    @Override
    public void setWidth(int side) {
        this.width = side;
        this.height = side;      // a square keeps its sides equal
    }

    @Override
    public void setHeight(int side) {
        this.width = side;
        this.height = side;
    }
}

The Square carefully keeps its two sides equal. It looks correct. Now see what happens in code that was written for rectangles:

Code written for any Rectangle
void resize(Rectangle r) {
    r.setWidth(5);
    r.setHeight(4);
    // A caller reasonably expects 5 x 4 = 20
    System.out.println(r.area());
}

resize(new Rectangle());   // prints 20
resize(new Square());      // prints 16

For a Rectangle the answer is 20. For a Square it is 16, because setHeight(4) also changed the width to 4. The caller wrote perfectly reasonable code and got a surprise. The child broke a promise that the parent made: "width and height can be set independently".

A Square that extends Rectangle breaks the promise of independent width and height; separate classes behind a Shape interface keep the promise
Fig. 1 · A Square that extends Rectangle breaks the promise of independent width and height; separate classes behind a Shape interface keep the promise

The lesson is important: "is a" in real life is not the same as "is a" in code. In geometry a square is a rectangle. But in code, what matters is behaviour. A mutable Square does not behave like a mutable Rectangle, so it should not inherit from it.

Loading the rest of the lesson…