DEV Community

yogini16
yogini16

Posted on • Updated on

Liskov Substitution principle is EASY

The Liskov Substitution Principle (LSP) is a principle in object-oriented programming that states that objects of a superclass should be able to be replaced with objects of a subclass without affecting the correctness of the program. In other words, if a program is using a base class, it should be able to use any of its derived classes without knowing it and still work correctly. This principle is related to the concept of polymorphism and is one of the five principles of SOLID programming.

The LSP is based on the idea that a subclass should be a subtype of its superclass, meaning it should be a valid replacement for an object of the superclass. For this to be true, the subclass must obey the contracts and constraints of the superclass, including its method signatures, preconditions, and postconditions. This means that the methods of the subclass should have the same name, return type, and parameters as the methods of the superclass and should not add any new constraints or require any more information than the superclass methods.

Here is an example of Liskov Substitution Principle:


class Rectangle
{
    public virtual int Width { get; set; }
    public virtual int Height { get; set; }

    public int Area()
    {
        return Width * Height;
    }
}

class Square : Rectangle
{
    public override int Width
    {
        get { return base.Width; }
        set { base.Width = base.Height = value; }
    }

    public override int Height
    {
        get { return base.Height; }
        set { base.Width = base.Height = value; }
    }
}

Enter fullscreen mode Exit fullscreen mode

In this example, Square class is derived from Rectangle class and it is Liskov Substitution Principle compliant, because it is a valid replacement for the objects of the Rectangle class and it does not add any new constraints or require any more information than the methods of Rectangle class.

It helps to follow SOLID principles in the software design, making software more maintainable, extensible and testable.

LSP is actually a subset of polymorphism.
Refer below image for more clarification.

Image description

If we would have implemented area of Square as Side * Side, I would have given wrong results for calculating rectangle's area.
In this example, a square us a special type of rectangle having all sides same. So a square IS-a rectangle. Hence a square should be IS-Substitutable for a rectangle.

So for making child such a that, we will always be able to get the parent in its expected normal form is Liskov substitution.

Seems Simple... Right?

Top comments (2)

Collapse
 
sloan profile image
Sloan the DEV Moderator

Just a heads up that you can add highlighting to the code blocks if you'd like. Just change:

code block with no colors example

... to specify the language:

code block with colors example

More details in our editor guide!

Collapse
 
yogini16 profile image
yogini16

That's awesome. Thank you @sloan