Liskov Substitution
Java Example
Bad Design (Violating LSP)
class Rectangle {
int width, height;
void setWidth(int width) {
this.width = width;
}
void setHeight(int height) {
this.height = height;
}
int getArea() {
return width * height;
}
}
class Square extends Rectangle {
void setWidth(int width) {
this.width = width;
this.height = width; // Violates LSP
}
void setHeight(int height) {
this.width = height;
this.height = height; // Violates LSP
}
}Good Design (Following LSP)
Python Example
Bad Design (Violating LSP)
Good Design (Following LSP)
Last updated