Liskov Substitution Principle

If you’ve been following my blog,you’ll know that we have been covering the SOLID Principles,in my previous posts i covered The Single Responsibility Principle,the “S” in the SOLID Acronym,then we covered The Open Closed Principle,today  my focus will be on The Liskov Substitution Principle,the “L” in the SOLID Principles acronym.

What is LSP?

This principle is credited to Barbara Liskov,she wrote this principle in 1988 and she said:

“What is wanted here is something like the following substitution property: If for each object o1
of type S there is an object o2 of type T such that for all programs P defined in terms of T, the
behaviour of P is unchanged when o1 is substituted for o2 then S is a sub-type of T.”

In simple terms the above statement means:

“Subtypes must be substitutable for their base types.”

or more simply:

“It means that we must make sure that new derived classes are extending the base classes without changing their behaviour” 

A Simple Example Using C#

We’ll use the classic Rectangle-Square problem to demonstrate this principle. Let’s imagine that we need to find the area of any rectangle.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace SOLIDPrinciplesDemo
{
  // If any module is using a Base class then the reference to that Base class can be replaced with a Derived class without affecting the functionality of the module.
  // Or
  // While implementing derived classes, one needs to ensure that, derived classes just extend the functionality of base classes without replacing the functionality of base classes.

class Rectangle
{
 protected int mWidth = 0 ;
 protected int mHeight = 0;

public virtual void SetWidth(int width)
{
  mWidth = width;
}

public virtual void SetHeight(int height)
{
  mHeight = height;
}


public virtual int GetArea()
{
  return mWidth * mHeight;
}
}


  // While implementing derived class if one replaces the functionality of base class then,
  // it might result into undesired side effects when such derived classes are used in existing program modules.
class Square : Rectangle
{
  // This class modifies the base class functionality instead of extending the base class functionality
  // Now below methods implementation will impact base class functionality.
public override void SetWidth(int width)
{
  mWidth = width;
  mHeight = width;
}

public override void SetHeight(int height)
{
  mWidth = height;
  mHeight = height;
}

}


class LiskovSubstitutionPrincipleDemo
{

private static Rectangle CreateInstance()
{
  // As per Liskov Substitution Principle "Derived types must be completely substitutable for their base types".
  bool SomeCondition = false;

if (SomeCondition == true)
{
  return new Rectangle();
}
else
{
  return new Square();
}
}

public static void LSPDemo()
{
  Console.WriteLine("\n\nLiskov Substitution Principle Demo ");
  Rectangle RectangleObject = CreateInstance();

  // User assumes that RectangleObject is a rectangle and (s)he is able to set the width and height as for the base class
  RectangleObject.SetWidth(5);
  RectangleObject.SetHeight(10);
  // Now this results into the area 100 (10 * 10 ) instead of 50 (10 * 5).
  Console.WriteLine("Liskov Substitution Principle has been violated and returned wrong result : " + RectangleObject.GetArea());
  // So once again I repeat that sub classes should extend the functionality, sub classes functionality should not impact base class functionality.
}
}
}

I know the example above doesn’t exhaust the LSP principle,but im sure it gave you an idea what the principle stands for.

Benefits

This principle aims to keep functionality intact. The main purpose is to guarantee that objects lower in a relational hierarchy can be treated as though they are objects higher in the hierarchy. Basically, any child class should be able to do anything the parent can do.

Further Reading

1.Data Abstraction and Hierarchy,” Barbara Liskov, SIGPLAN Notices, 23(5) (May 1988)

2.Bertrand Meyer, Object-Oriented Software Construction, 2d. ed., Prentice Hall, 1997

3.Rebecca Wirfs-Brock et al. , Designing Object-Oriented Software, Prentice Hall,
1990.

4.Agile.Principles.Patterns.and Practices.In.C#[Robert.C.Martin]

That’s it for this week,next i will be covering Interface Segregation Principle(ISP),the letter “I” in the SOLID acronym.

****Happy Holidays and be safe****

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.