Quick revision
The short answer
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.
What to remember
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.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
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.
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.
| Call | Rectangle (w x h) | Square (w x h) |
|---|---|---|
| new ...() | 0 x 0 | 0 x 0 |
setWidth(5) | 5 x 0 | 5 x 5 |
setHeight(4) | 5 x 4 | 4 x 4 |
area() | 20 | 16 |
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.
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.
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.
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.
- Official documentation
- The Java Language Specification, Java SE 21, Java SE 21 API documentation
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/