SOLID – Liskov Substitution Principle

Definition :

it states that derived class must be substitutable for their base classes. which means that the subtypes must be replaceable for their super type references without affecting the program execution.

This principle was early described by Barbra LiskovĀ in 1988.

LSP

When we were learning about object oriented programming, inheritance is usually described as an “is a” relationship, like If a penguin “is a” bird, then the Penguin class should inherit from the Bird class. The “is a” technique of describing inheritance relationship is simple and useful, but occasionally results in bad use of inheritance.

I’ll use the square and rectangle example to demonstrate the principle:
problem:

we have a Rectangle base Class that have two properties width and height , and subclass called Square that have the same two properties Height and Width and a method called SideLength that we use to set the values of height and width properties to make them equal.

28-5-2013 4-10-02 PM

Continue reading

Advertisements