Liskov Substitution Principle
Liskov Substitution Principle: Subtypes must be substitutable for their base types. Here is an example of a class hierarchy that violates the Liskov Substitution Principle:
class Rectangle:
def __init__(self, width, height):
self.width = width
self.height = height
def area(self):
return self.width * self.height
class Square(Rectangle):
def __init__(self, side):
self.width = side
self.height = side
The Square class is a subtype of Rectangle, but it is not substitutable because it has different behavior (the width and height must be equal). To fix this, we can change the implementation of the Square class to adhere to the Liskov Substitution Principle:
class Rectangle:
def __init__(self, width, height):
self.width = width
self.height = height
def area(self):
return self.