Scrivio Interview library
ConceptFoundations6 min read · updated 12 November 2026

Why is the classic Square extends Rectangle example a Liskov substitution violation? Show code that breaks.

Square extends Rectangle violates Liskov substitution because a mutable Rectangle promises that its width and height can be set independently, and a Square cannot keep that promise without breaking its own rule that both sides are equal. Code such as resize(Rectangle r) that sets width 5 and height 4 and expects area 20 gets 16 for a Square. The fix is to make shapes immutable or to stop relating them by inheritance and give them a common Shape interface.

Why this question is asked

The square and rectangle example is the classic way to show that is-a in the real world does not mean extends in code. It matters in real work because the same mistake appears with less obvious types: a subclass that looks like a special case but quietly changes what the parent's methods promise. Interviewers ask it because it tests whether we think of inheritance in terms of behaviour, not taxonomy. In this answer, we will recap the Liskov substitution principle, build the two classes, write the code that breaks, explain exactly which promise is broken, and then look at two fixes.

1A Quick Recap of Liskov Substitution

The Liskov substitution principle says that if code works with an object of type T, it must keep working when we pass it an object of any subtype of T. A subtype is allowed to do more, but it must not do less, and it must not surprise callers that only know about T.

We can state this more precisely with two terms. A precondition is what must be true before a method is called, for example that an argument is positive. A postcondition is what the method promises after it returns, for example that a field now holds the new value. A subtype may weaken preconditions, accepting more inputs, and may strengthen postconditions, promising more. It must never strengthen a precondition or weaken a postcondition, because callers written against the parent rely on both.

2Building Rectangle and Square

Mathematically a square is a rectangle, so inheritance looks natural. Here are the two classes, with Square forcing both sides to stay equal.

Java
class Rectangle {
protected int width, height;
void setWidth(int w) { width = w; } // promise: only width changes
void setHeight(int h) { height = h; } // promise: only height changes
int area() { return width * height; }
}
class Square extends Rectangle {
@Override void setWidth(int w) { width = w; height = w; } // keeps sides equal
@Override void setHeight(int h) { width = h; height = h; }
}

Each class is correct on its own. A Rectangle behaves like a rectangle, and a Square is always square. The problem only appears when code written for Rectangle receives a Square.

3Reproducing the Problem

This method was written against Rectangle. It sets the sides and checks the result, which is a perfectly reasonable thing to expect from a rectangle.

Java
static void resize(Rectangle r) {
r.setWidth(5);
r.setHeight(4);
assert r.area() == 20 : "area was " + r.area();
}
resize(new Rectangle()); // passes: width 5, height 4, area 20
resize(new Square()); // AssertionError: area was 16 (run with java -ea)
State after each call in resize()
CallRectangle (w x h)Square (w x h)
new ...()0 x 00 x 0
setWidth(5)5 x 05 x 5
setHeight(4)5 x 44 x 4
area()2016
setHeight(4) on a Square also changes the width, which no Rectangle caller expects.

With a Square, setWidth(5) sets both sides to 5, and then setHeight(4) sets both sides to 4. The area is 16, not 20. Note that Java assertions only run when the JVM starts with -ea; in real code the same failure shows up as a wrong layout, a wrong price or a failing test.

4Which Promise Does Square Break?

Rectangle's setHeight carries a postcondition that callers rely on: after setHeight(h), the height is h and the width is unchanged. Square's override weakens that postcondition, because it changes the width too. Seen from the other side, Square also strengthens a precondition: it can only behave correctly if callers always pass equal values, and Rectangle callers never had to know that.

So the violation is not in Square's code, which does exactly what a square should. It is in the subtype relationship. Square cannot honour Rectangle's contract, so it is not a behavioural subtype of a mutable Rectangle, whatever geometry says. Notice that the problem comes from the setters. The is-a relationship holds for the shapes; it breaks for the mutable objects.

5Fix 1: Make the Shapes Immutable

If shapes cannot be changed after they are created, there are no setters with a postcondition to break. Instead of changing a rectangle, we create a new one.

Java
record Rectangle(int width, int height) {
Rectangle withWidth(int w) { return new Rectangle(w, height); }
Rectangle withHeight(int h) { return new Rectangle(width, h); }
int area() { return width * height; }
}
Rectangle r = new Rectangle(1, 1).withWidth(5).withHeight(4);
System.out.println(r.area()); // 20

With immutability, asking a square for a different width simply returns a rectangle that is no longer square, which is the honest answer. Records cannot extend other classes, so if we still want a Square type, we model it next to Rectangle rather than under it, which leads to the second fix.

6Fix 2: A Common Shape Interface Instead of Inheritance

Most callers do not need to resize a shape at all; they need its area or perimeter. We can give both shapes a common interface for what they really share and keep them otherwise unrelated.

Java
sealed interface Shape permits Rectangle, Square {
int area();
}
record Rectangle(int width, int height) implements Shape {
public int area() { return width * height; }
}
record Square(int side) implements Shape {
public int area() { return side * side; }
}
List<Shape> shapes = List.of(new Rectangle(5, 4), new Square(4));
shapes.forEach(s -> System.out.println(s.area())); // 20, then 16

Now no caller can write resize(Rectangle) and receive a Square, because a Square is not a Rectangle in the type system. Code that needs independent sides takes a Rectangle; code that only needs an area takes a Shape. The types now say exactly what each caller may rely on.

7Follow-up questions

Is there a version of Square extends Rectangle that is acceptable?

Yes, if Rectangle is immutable and its methods only report on the shape, such as area and perimeter. Without setters there is no postcondition about independent sides, so a Square can satisfy every promise Rectangle makes. The violation exists only because the mutable Rectangle promises independent change.

Could Square's setters throw an exception if the sides differ?

That would make things worse. It strengthens the precondition, so callers that worked with any Rectangle now fail with an exception. Throwing in an override where the parent does not is one of the clearest signs of a Liskov violation.

How would you detect this kind of problem in a real codebase?

Contract tests help: write the tests that describe the parent's behaviour once and run them against every subclass. A Square would fail the test that sets width and height independently. Overrides that ignore arguments, change other fields or throw new exceptions are also worth flagging in review.

8Conclusion

In this answer, we saw that Square extends Rectangle breaks Liskov substitution because a mutable Rectangle promises that width and height change independently, and a Square cannot keep that promise. The resize() example gets an area of 16 instead of 20. The problem lies in the subtype relationship, not in either class, and it disappears when shapes are immutable or when both implement a common Shape interface instead of inheriting from each other.

What to say in the interviewAbout 90 seconds aloud

Open with

The Liskov principle says code that works with a type must keep working with any of its subtypes, so a subtype can't strengthen preconditions or weaken postconditions. A mutable Rectangle has setWidth and setHeight, and callers rely on the promise that setting the height leaves the width alone.

Then, if they want more

Square has to keep both sides equal, so its setters change both. If I write resize(Rectangle r) that sets width to 5, height to 4, and asserts the area is 20, it passes for a Rectangle, but for a Square the second call sets both sides to 4 and the area is 16.

So Square weakens the postcondition of setHeight, or, put the other way, it only works if callers always pass equal sides, which is a stronger precondition. The interesting point is that neither class is buggy; the inheritance relationship is the problem. Geometry says a square is a rectangle, but behaviourally a mutable square is not a mutable rectangle.

I'd fix it in one of two ways. If shapes are immutable, records with withWidth methods that return new objects, there's no setter contract to break. Or I don't relate them by inheritance at all: both implement a Shape interface with area, maybe a sealed interface, and code that needs independent sides asks for a Rectangle specifically.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "Why is the classic Square extends Rectangle example a Liskov substitution violation?...": https://scrivio-interview-library.netlify.app/java/oop-principles/56303c5516/