Alex Murphy

Jun 21, 2020

3 min read

Liskov Substitution Principle

In this article, I am going to discuss the Liskov Substitution Principle with a real-time example. The Letter L in SOLID stands for Liskov Substitution Principle which is also known as LSP. As part of this article, we are going to discuss the following pointers in detail.

Here is the list of the blogs in this series:

  1. Understand SOLID Principle
  2. Single Responsibility Principle
  3. Open Close Principle
  4. Interface Segregation Principle
  5. Dependency Inversion Principle

What is the Liskov Substitution Principle?

The Liskov Substitution Principle is a Substitutability principle in object-oriented programming Language. This principle states that, if ‘A’ is a base class of ‘a’, then objects of type ‘a’ should be replaced with the objects of type ‘A’.

In other words, we can say that objects in an application should be replaceable with the instances of their subtypes without modifying the correctness of that application.

For example, the father is a teacher whereas his son is a doctor. So here, in this case, the son can’t simply replace his father even though both belong to the same family.

Example without using the Liskov Substitution Principle.

Let us first understand one example without using the Liskov Substitution Principle. In the following example, first, we create the Apple class with the method getColor. Then we create the Orange class which inherits the Apple class as well as overrides the getColor method of the Apple class. The point is that an Orange cannot be replaced by an Apple, which results in printing the color of Apple as Orange as shown in the below example.

public class LiskovSubstituteTest {
public static void Main(string[] args) {

Apple apple = new Orange();
System.out.println(apple.getColor());
}
}
public class Apple { public String getColor() {
return "Red";
}
}
public class Orange : Apple {

public String getColor() {
return "Orange";
}
}

Example using the Liskov Substitution Principle

Let’s modify the previous example to follow the Liskov Substitution Principle. Here, first, we need a generic base class such as Fruit for both Apple and Orange. Now you can replace the Fruit class object with its subtypes either Apple and Orange and it will behave correctly.

Here we create one Fruit class as abstract class, you can make it as interface also.

public abstract class Fruit {
public abstract String getColor();
}

Now we will extend this class into Apple and Orange classes like below.

public class Apple extends Fruit {    @Override
public String getColor() {
return "Red";
}
}
public class Orange extends Fruit { @Override
public String getColor() {
return "Orange";
}
}

Now if we call getColor method by following way then we can get perfect result

public class LiskovSubstituteTest {

public static void Main(String[] args) {

Fruit fruit = new Orange();
System.out.println(fruit.getColor());

fruit = new Apple();
System.out.println(fruit.getColor());
}
}

Now, run the application and it should give the output as expected. Here we are following the Liskov Substitution Principle as we are now able to change the object with its subtype.

In the next article, I am going to discuss the Interface Segregation Principle with a real-time example.

Thanks for reading this article ❤

If I got something wrong? Let me in the comments. I would love to improve.

Clap 👏 If this article helps you.

Follow me on Twitter.