
Summary: We looked at what is Liskov Substitution Principle and saw an example of it in Java. This is exactly in line with the Liskov Principle we just saw above. This way when the derived class’s(or the sub-type’s) object is used in place of the parent/super-class, then the overridden functionality is executed on executing the same functionality. Rather, we should override the original class and implement the functionality to be changed in the overriding class. to modify what a class does, we should not change the original class.
#Liskov substitution principle full
Read the full tutorial on Open Closed Principle) says that a class should be open for extension and closed for modification. Relation of Liskov Substitution Principle with Open Closed Principle The principle actually expects it to work smoothly. This is a typical violation scenario of Liskov Substitution Principle as assigning a subtype object to a super type reference doesn’t work which is not in line with the principle. This is known as the Circle-Ellipse Problem. However, calling any of these two methods on an object of type circle inside a reference of type ellipse would lead to a circle no longer being a circle as in a circle the length of the major and minor axes have to be equal. Lets say we have methods setLengthOfAxisX() and setLengthOfAxisY() through which the length of the two axes X & Y of an ellipse can altered. the length of the two axes of an ellipse can be changed.


One inherent functionality of an ellipse is that its stretchable. So, all the methods in Ellipse can/could be invoked on this object of Circle which is stored in it. In this case an object of type Circle can be assigned to a reference of type Ellipse. Lets now use the Liskov Substitution Principle. In terms of classes if we make a class Ellipse then Circle class will be a child of Ellipse class i.e. Liskov Violation Scenario Example: The Circle-Ellipse ProblemĪll circles are inherently ellipses with their major and minor axes being equal. This is exactly what the Liskov Substitution Principle also states – subtype objects can replace super type objects without affecting the functionality inherent in the super type.

Referring to the diagram above, it is possible to invoke methods like getSpeed() & getCubicCapacity() on a Vehicle reference which actually holds a Bus/Car object.The actual object’s overridden implementation of these methods will be actually invoked. All the functionality which is inherent in base class Vehicle, and is acquired by Bus and Car via inheritance, can be invoked on a reference of type Vehicle. Now, we can assign an object of type Car or that of type Bus to a reference of type Vehicle.
