Square and rectangle
The classic example of a problem that violates the Liskov Substitution Principle is the Circle-Ellipse problem, sometimes called the Square-Rectangle problem.
By all means, it seems like you should be able to define a class Rectangle
, with attributes h
and w
(for height and width), and then define a class Square
that inherits from the Rectangle
. After all, a square "is-a" rectangle!
Unfortunately, this intuition doesn't apply to object-oriented design.
This exercise is part of the course
Object-Oriented Programming in Python
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Define a Rectangle class
____
# Define a Square class
____