xxxxxxxxxx
- The code is loosely coupled and avoids unnecessary entanglements.
Without LSP, when a subclass cannot substitue its parent class,
we have to write multiple if-else statements to determine the class or
the type to handle certain cases differently.
Changes should be applied in many places
- Violtion - Square inheriting from a Rectangle
xxxxxxxxxx
class Test {
static void getAreaTest(Rectangle r) {
int width = r.getWidth();
r.setHeight(10);
System.out.println("Expected area of " + (width * 10) + ", got " + r.getArea());
}
public static void main(String[] args) {
Rectangle rc = new Rectangle(2, 3);
getAreaTest(rc);
Rectangle sq = new Square();
sq.setWidth(5);
getAreaTest(sq);
}
}