The Liskov Substitution Principle (LSP) is a principle in object-oriented programming that states that objects of a superclass should be able to be replaced with objects of a subclass without affecting the correctness of the program.
In other words, if a program is written using objects of a superclass, it should be able to work correctly when those objects are replaced with objects of a subclass, as long as the subclass objects adhere to the contract defined by the superclass. This means that the subclass objects should be substitutable for the superclass objects without causing any issues in the program.
The LSP is one of the SOLID principles of object-oriented design, which are a set of guidelines for designing maintainable and scalable software. The SOLID principles were introduced by Robert C. Martin as a way to improve the design of object-oriented software by making it more flexible, reusable, and maintainable.
The LSP is an important principle to follow because it helps to ensure that subclasses are properly implemented and behave as expected when used in a program. By adhering to the LSP, developers can create more reliable and maintainable software that is easier to modify and extend over time.
Here is an example of a real-time implementation of the Liskov Substitution Principle (LSP) in a program:
public class Rectangle {
protected int width;
protected int height;
public void setWidth(int width) {
this.width = width;
}
public void setHeight(int height) {
this.height = height;
}
public int getArea() {
return width * height;
}
}
public class Square extends Rectangle {
@Override
public void setWidth(int width) {
super.setWidth(width);
super.setHeight(width);
}
@Override
public void setHeight(int height) {
super.setHeight(height);
super.setWidth(height);
}
}
In this example, the Rectangle
class represents a rectangle and has width
and height
properties, as well as a getArea
method that calculates the area of the rectangle. The Square
class extends the Rectangle
class and represents a square, which is a special type of rectangle with equal sides.
The Square
class overrides the setWidth
and setHeight
methods of the Rectangle
class, setting both the width
and height
properties to the same value whenever either of these properties is set. This ensures that the Square
class maintains the invariant that all sides are equal, and that the Square
object behaves correctly as a square.
Because the Square
class adheres to the contract defined by the Rectangle
class and maintains the invariant of having equal sides, it is substitutable for the Rectangle
class. This means that a program that is written using objects of the Rectangle
class should be able to work correctly when those objects are replaced with objects of the Square
class, without causing any issues in the program.
Liskov Substitution Principle
1. Preconditions cannot be strengthen in subsystem(child classes) but they can be weaken
if parent class contains `if(tittle ==””) {}` ,you cannot add one more condition `if(tittle==”” && !char.IsUpper(tittle,0))` in child class
2. Postconditions cannot be weakened
How to avoid
The bigger the project the harder it is to spot LSP breaks
Can I use base and child class interchageably
Did I think about all corner cases of implemented code
You will get better at it seeing such code smells
