Liskov’s Substitution Principle (LSP)

By | September 30, 2020

This principle is a variation of the open-closed principle. It says:

“Derived or subtypes must be completely substitutable for their base types”

That means that the classes may be created by extending our class should be able to fit in the application without any failure. This requires the objects of your subclasses to behave in the same way as the objects of your super Class. This is mostly seen in places where we needed run-time type identification and then cast it to the appropriate reference type.

Example

If class X is a subtype of class Y, then we should be able to replace with without disrupting the behavior of our program.

Why is this principle required?

This avoids misusing the inheritance OOPs feature. This principle helps us conform to the “is-a” relationship. We must say that subclasses must fulfill a contract defined by the base or super Class. For example, it’s tempting to say that a circle is a type of ellipse but circles don’t have two foci or major/minor axes.

You May Also Like:

Adapter Pattern
Decorator Pattern
Facade Pattern
Proxy Pattern
Composite Pattern
Flyweight Pattern
Bridge Pattern

That’s all about Java Liskov’s Substitution Principle (LSP)
If you have any feedback or suggestion please feel free to drop in below comment box.

Leave a Reply

Your email address will not be published. Required fields are marked *