L of S.O.L.I.D. Liskov’s Substitution principle


This is the third in the 5 principles which are defined as an acronym S.O.L.I.D. This principle is further having an acronym called L.S.P. which stands for Liskov’s Substitution Principle. This principle was introduced by Barbara Liskov. A definition from wikipedia about this principle says :

If S is a subtype of T, then objects of type T may be replaced with objects of type S (i.e., objects of type S may substitute objects of type T) without altering any of the desirable properties of that program (correctness, task performed, etc.).

In simple technical language, it means, when we have a base class and child class relationship, then, if we can successfully replace the object/instance of parent class with object/instance of the child class, without affecting the results which we get with the base class instance, then the two classes conform to this principle. Might sound bit confusing, let’s discuss with an example.

To understand this principle, we will use the same example of the Rectangle-Square hierarchy which you might have seen a lot, where Rectangle is the base class and Square is the derived class. Our discussion focus will be on how the code violates the L.S.P., rather then the actual code is or should have been written for it. The code for this example is below :

Liskov Substitution Principle

And, the client code is :

Liskov Substitution Principle

As per the definition of this principle, if this example does not violates the L.S.P., then the getArea method in the base class will give the same area of a rectangle, i.e. whether we execute this method with the instance of the Rectangle class or the Square class, we should get area as 120. But it is not and this is how it violates L.S.P.

Where is the issue ?

At first, it might seem that the code is fine(until we run it). In-fact, the code will work fine if we attempt to get the area using the instance of the rectangle class. But if we use instance of square class, the result is different. The reason being, when we write

Rectangle _iShape = new Square();

_iShape holds reference of the the type Square. Next, when we execute the setWidth & setHeight methods, it executes the methods of the Square class, which sets height = width (as per the logic of the square has length = breadth) and when the getArea is executed, it calculates incorrect area.

This is the reason that this code violates the Liskov Substitution Principle, when we replaced the object/instance of the Rectangle, with that of Square and this is what this principle is all about i.e.

If S is a subtype of T, then objects of type T may be replaced with objects of type S (i.e., objects of type S may substitute objects of type T) without altering any of the desirable properties of that program (correctness, task performed, etc.).

So this is about the concept of Liskov Substitution Principle. Hope you enjoyed reading it…!!!

Advertisements

About Jasminder

.Net developer and blogger by profession, keen to learn new technologies, love nature, music and cricket.
This entry was posted in SOLID Principles and tagged . Bookmark the permalink.

I have a suggestion..

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s