Liskov Substitution Principle (LSP) is the third amongst the five design principles stipulated by SOLID. It was introduced by Barbara Liskov in 1987.
Liskov Substitution Principle
Functions that use pointers or references to base classes must be able to use objects of derived classes without knowing it.
What it means?
It basically means that every subclass/derived class should be substitutable by their base/parent class.
In other words, a subclass should override the parent class methods in a way that it does not break functionality from a client’s point of view.
Let’s understand better with an example
Consider a Rectangle class with basic properties of Height and Width.
Now, we can calculate the area of this rectangle in the following manner…
The output of this program would be …
All good so far. Now supposing we wish to also define a Square class. Geometrically, a square is a form of rectangle. Hence, we can define the Square class as a subclass of Rectangle as ..
The above code is self-explanatory, in the sense that for a square both height and width should be equal.
We can now update the Main method as ..
The output now comes up as expected as …
Violating the Liskov Substitution Principle
Now as per Liskov Substitution Principle, we should be able to replace the Square class reference with Rectangle class reference in the Main method and still the area of the square should remain unchanged. Let’s try this …
The output of this comes out as …
Ideally, the area should have remained unchanged as 16, however it has become 0. Hence, Liskov Substitution Principle is violated.
Refactoring and aligning to the Liskov Substitution Principle
To set this right, we need to override the Weight and Height properties in the base class with the ones in derived class. Hence, we should replace the new keyword in subclass with override and mark the respective properties in base class as virtual.
The refactored code would now look as …
If we now run the above code …
This time the output is as expected and aligned with the Liskov Substitution Principle. You may download the code from my github repository here