A child must keep the parent's promises
After this: Watch a square break code written for rectangles, and fix the family with an interface.
On this page · 6 sections · 3 checks
In school geometry a square is a rectangle: a rectangle whose four sides are equal. So it seems natural to make Square a child class of Rectangle. This lesson shows code that works for every rectangle and gives a wrong answer for a square, and the rule that explains why.
A square that is a rectangle
Rectangle has a width w and a height h, two methods that set them, and area. Square is its child. A square must keep its sides equal, so it overrides both setters: setting the width sets the height too, and the other way round.
class Rectangle: def __init__(self, w, h): self.w = w self.h = h
def set_width(self, w): self.w = w
def set_height(self, h): self.h = h
def area(self): return self.w * self.h
class Square(Rectangle): def __init__(self, side): super().__init__(side, side)
def set_width(self, w): self.w = w self.h = w
def set_height(self, h): self.w = h self.h = h
def resize(r): r.set_width(5) r.set_height(4) return r.area()
print(resize(Rectangle(2, 3)))print(resize(Square(2)))20 16
class Rectangle { protected int w; protected int h;
Rectangle(int w, int h) { this.w = w; this.h = h; }
void setWidth(int w) { this.w = w; }
void setHeight(int h) { this.h = h; }
int area() { return w * h; }}
class Square extends Rectangle { Square(int side) { super(side, side); }
void setWidth(int w) { this.w = w; this.h = w; }
void setHeight(int h) { this.w = h; this.h = h; }}
public class Main { static int resize(Rectangle r) { r.setWidth(5); r.setHeight(4); return r.area(); }
public static void main(String[] args) { System.out.println(resize(new Rectangle(2, 3))); System.out.println(resize(new Square(2))); }}20 16
resize was written for rectangles. It sets the width to 5 and the height to 4, and returns the area. Anyone who reads it expects 5 × 4 = 20.
- For
Rectangle(2, 3): the width becomes 5, then the height becomes 4. The area is20. - For
Square(2): setting the width to 5 sets both sides to 5. Setting the height to 4 sets both sides to 4. The area is 4 × 4 =16.
Nothing crashed. The square kept its own rule, equal sides, and in doing so it broke a promise that Rectangle makes: setting the height leaves the width alone. Code that trusted that promise now returns a wrong number, and nothing warns you.
- Square inherits from Rectangle
The trace below runs a short version of the program. The task asks you to watch the two fields of each object after every setter.
- Run the programRun it and step through. resize is called twice: first with a rectangle, then with a square. Inside each call, read the fields w and h of the object after the width is set and again after the height is set.
class Rectangle: def __init__(self, w, h): self.w = w self.h = h
def set_width(self, w): self.w = w
def set_height(self, h): self.h = h
class Square(Rectangle): def set_width(self, w): self.w = w self.h = w
def set_height(self, h): self.w = h self.h = h
def resize(r): r.set_width(5) r.set_height(4) return r.w * r.h
print(resize(Rectangle(2, 3)))print(resize(Square(2, 2)))run the program to see its boxes
nothing printed yetPress Run it, then step through the program one line at a time.
- lines
- 0
- prints
- 0
- calls
- 0
The rule
A child object must work anywhere its parent works. Code written for the parent should give the right answer when it is handed a child, without knowing that it is a child. This is the , the L of SOLID. It is named after the computer scientist Barbara Liskov, who described the idea in a talk in 1987. To substitute means to put one thing in the place of another.
The "is a" sentence test from "Composition over inheritance" is not enough on its own here. "A square is a rectangle" is true in geometry. In code the question is whether a Square object behaves like a Rectangle object, setters included, and it does not.
The fix: siblings, not parent and child
Take the inheritance away. Rectangle and Square both implement a small interface, Shape, with one method, area. Neither has setters: each shape gets its sizes when it is made.
- Rectangle implements Shape
- Square implements Shape
from abc import ABC, abstractmethod
class Shape(ABC): @abstractmethod def area(self): pass
class Rectangle(Shape): def __init__(self, w, h): self.w = w self.h = h
def area(self): return self.w * self.h
class Square(Shape): def __init__(self, side): self.side = side
def area(self): return self.side * self.side
for shape in [Rectangle(5, 4), Square(4)]: print(type(shape).__name__, shape.area())Rectangle 20 Square 16
import java.util.List;
interface Shape { int area();}
class Rectangle implements Shape { private int w; private int h;
Rectangle(int w, int h) { this.w = w; this.h = h; }
public int area() { return w * h; }}
class Square implements Shape { private int side;
Square(int side) { this.side = side; }
public int area() { return side * side; }}
public class Main { public static void main(String[] args) { for (Shape shape : List.of(new Rectangle(5, 4), new Square(4))) { System.out.println(shape.getClass().getSimpleName() + " " + shape.area()); } }}Rectangle 20 Square 16
Now no code can treat a square as a rectangle, so no code can be surprised by one. The loop asks each shape for its area, and each answers correctly: Rectangle 20 and Square 16.
Run it
Swap the two setter lines inside resize and run it again. The rectangle still gives 20, but the square now gives 25, because the last setter decides both of its sides.
Check yourself
Which change follows the Liskov substitution principle for Rectangle and Square?
Follow the square inside resize, which starts with both sides 2. What are its two fields, w and h, after the width is set to 5, and then after the height is set to 4?
The same Rectangle and Square, and a new function that sets the width to 6 and then the height to 3. What does this print?
class Rectangle: def __init__(self, w, h): self.w = w self.h = h
def set_width(self, w): self.w = w
def set_height(self, h): self.h = h
def area(self): return self.w * self.h
class Square(Rectangle): def __init__(self, side): super().__init__(side, side)
def set_width(self, w): self.w = w self.h = w
def set_height(self, h): self.w = h self.h = h
def stretch(r): r.set_width(6) r.set_height(3) return r.area()
print(stretch(Rectangle(1, 1)), stretch(Square(1)))class Rectangle { protected int w; protected int h;
Rectangle(int w, int h) { this.w = w; this.h = h; }
void setWidth(int w) { this.w = w; }
void setHeight(int h) { this.h = h; }
int area() { return w * h; }}
class Square extends Rectangle { Square(int side) { super(side, side); }
void setWidth(int w) { this.w = w; this.h = w; }
void setHeight(int h) { this.w = h; this.h = h; }}
public class Main { static int stretch(Rectangle r) { r.setWidth(6); r.setHeight(3); return r.area(); }
public static void main(String[] args) { System.out.println(stretch(new Rectangle(1, 1)) + " " + stretch(new Square(1))); }}When not to bother
- Say the Liskov substitution principle: a child object must work anywhere its parent works.
- Follow resize on a rectangle (20) and on a square (16), and say which promise of Rectangle the square breaks.
- Say why "a square is a rectangle" is true in geometry but not for a Rectangle class with setters.
- Fix the family by making Rectangle and Square siblings under a Shape interface with no setters.