Liskov Substitution Principle

In this article, we will discuss about Liskov Substitution Principle and how to implement it in Java.

 

What is Liskov substitution principle (LSP) ?

 

The Liskov substitution principle (LSP) is a particular definition of a subtyping relation, called (strong) behavioral subtyping, that was initially introduced by Barbara Liskov in a 1987 conference keynote address entitled Data abstraction and hierarchy:
 

  • if S is a subtype of T, then objects of type T in a program may be replaced with objects of type S without altering any of the desirable properties of that program

 
Liskov Substitution Principle photo
 
In order to follow this principle, we need to make sure that the subtypes can be substituted for the parent type.
 
Let’s look at the below example to understand it better.
 

package com.topjavatutorial;

public class Rectangle {
  
  double height;
  
  double width;
  

  public double getHeight() {
    return height;
  }

  public void setHeight(double height) {
    this.height = height;
  }

  public double getWidth() {
    return width;
  }

  public void setWidth(double width) {
    this.width = width;
  }

  public double getArea(){
    return height * width;
  }
}

 
Here we have a Rectangle class with properties height and width.
 
In geometry, a square is a rectangle with height same as it’s width.
 
So, lets create a Square class by extending the Rectangle class.
 
package com.topjavatutorial;

public class Square extends Rectangle {

  public void setHeight(double height) {
    this.height = height;
    this.width = height;
  }

  public void setWidth(double width) {
    this.height=width;
    this.width = width;
  }
}


 
We modified the setter methods for square since height and width will be equal for a Square.

 
The example above looks fine. You can create a square object and set both the height and width values using either of the setter methods, resulting in a square where all sides are equal.
 
The problem arises when you define a rectangle with different height and width values and then try to substitute that object using a Square object… you will get unexpected results as the Square object expects its height and width properties to have the same value all the time.
 
To test this, lets create a TestClass with main(), where we create a Rectangle reference, but place a Square instance in it (Since subclass object can be replaced where a superclass instance is expected).
 

package com.topjavatutorial;

public class TestClass {

  public static void main(String[] args) {
  Rectangle r = new Square();
  r.setHeight(4);
  r.setWidth(5);
  System.out.println(r.getArea());
  }

}


 

Output:

 

25.0

 

Since we are creating a Rectangle reference and calling getArea() on the rectangle, the expected result for the rectangle’s area should be 20. But this program returns the area as 25.
 
As per Liskov substitution principle, you should be able to substitute any of the child classes for its base class and the example of the rectangle/square clearly breaks that rule.
 
Subclasses should act as you would expect a base class to be used, without the need to downcast to check for specific subclass behavior.
 
 

You might like reading about SOLID principles here :

Single Responsibility Principle

Open Closed Principle

Interface Segregation Principle

 

Reference

 
Patterns, Principles, and Practices of Domain-Driven Design

Image referred from https://lostechies.com/derickbailey/2009/02/11/solid-development-principles-in-motivational-pictures/(Derick Bailey)
 
 

© 2016, https:. All rights reserved. On republishing this post, you must provide link to original post

3 comments

  1. Typo: area of rectangle should be 20 (not 25).

  2. […] Liskov Substitution Principle […]

  3. […] Liskov Substitution Principle   The Interface Segregation Principle (ISP) states that clients should not be forced to depend […]

Leave a Reply.. code can be added in <code> </code> tags

%d bloggers like this: