Liskov Substitution Principle

Reading Time: 2 minutes

solid-logo-Liskov

Liskov Substitution Principle is one of the five SOLID principles, LSP was introduced by Barbara Liskov. Liskov Substitution Principle defines that objects of a superclass must be replaceable with objects of its subclasses without breaking the application. It means that the objects of subclasses to behave in the same way as the objects of your superclasses.

For exmaple, class Dog is a subclass of class Animal, we should be able to pass an object of class Dog to any method that expects an object of class Animal and the method should not give any weird output in that case.

LSP extends the open-close principle and also focuses on the behavior of a superclass and its subtypes.

This is the expected behavior because when we use inheritance we assume that the child class inherits everything that the superclass has. The child class extends the behavior but never narrows it down.

Let’s look at an example that follows Liskov Substitution Principle rules:

class Vehicle {
    String name;
    int speed;
    public Vehicle(String name, int speed) {
        this.name = name;
        this.speed = speed;
    }
    public void start() {
        System.out.println(name + " is running with speed "+speed);
    }
}

We have a Vehicle class and start() method which prints the name and speed of the vehicle.

Now we decide to have some child classes as Cycle, Bike, and Car.

class Cycle extends Vehicle {
    public Cycle(String name, int speed) {
        super(name, speed);
    }
    @Override
    public void start() {
        super.start();
    }
}
class Bike extends Vehicle {
    public Bike(String name, int speed) {
        super(name, speed);
    }
    @Override
    public void start() {
        super.start();
    }
}
class Car extends Vehicle {
    public Car(String name, int speed) {
        super(name, speed);
    }
    @Override
    public void start() {
        super.start();
    }
}

Let’s create a main class to perform tests on the start() function. Test class has a startVehicle() method that takes Vehicle class object as an argument and performs the operation. Hence, you can provide any object of child classes for example object of Car, Bike, and Cycle class because these classes extend Vehicle class.

public class Test {
    public void startVehicle(Vehicle vehicle) {
        vehicle.start();
    }
    public static void main(String[] args) {
        Vehicle bike = new Bike("Bike", 60);
        Vehicle cycle = new Cycle("Cycle", 30);
        Vehicle car = new Car("Car", 80);
        Test test = new Test();
        test.startVehicle(cycle);
        test.startVehicle(bike);
        test.startVehicle(car);
    }
}

Output:

Cycle is running with speed 30
Bike is running with speed 60
Car is running with speed 80

Real world example of LSP:

liskov-substitution-principle-example

Written by 

Aasif Ali is a Software Consultant at Knoldus Inc. He has done Post Graduation from Quantum University Roorkee. He has the knowledge of various programming languages. He is passionate about Java development and curious to learn Java Technologies. He is always impatient and enthusiastic to learn new things. He is a quick learner, problem solver and always enjoy to help others. His hobbies are watching Sci-fi movies , Playing badminton and listening to songs.

Leave a Reply