Liskov Substitution Principle

What is the Liskov Substitution Principle?

The above principle focuses on the concept of inheritance and subtyping.

Definition. "Subtypes must be substitutable for their base types without altering the correctness of the program."

This principle ensures that derived classes (subtypes) can be used interchangeably with their base classes (superclasses) without causing issues.

Who invented this principle?

Barbara Liskov, an American computer scientist in the year 1985, published a paper on the above principle.

Key Requirements for implementation of Liskov

  1. "Is-a" Relationship: It emphasizes that the relationship between a base class and its derived class should maintain an "is-a" relationship. In other words, a derived class should be a specialization of the base class. For example, if you have a base class Vehicle and a derived class Car, a car is a type of vehicle.
  2. Behavioral Compatibility: Subtypes should exhibit behavioral compatibility with their base types. This means that the methods in the derived class should adhere to the same contract (preconditions and postconditions) as those in the base class. The derived class can strengthen the postconditions or weaken the preconditions but should not break them.

Fundamental of the Liskov Principle

Inheritance and Polymorphism: The Liskov Substitution Principle is closely related to inheritance and polymorphism. It encourages the proper use of these mechanisms to ensure that derived classes can be used interchangeably with their base classes.

Glossary

Subtyping: Subtyping in the context of LSP refers to creating a derived class that extends the functionality of the base class while preserving its expected behavior. It should not violate the assumptions made by code that uses the base class.

Violation

Violating the Principle can lead to unexpected and incorrect behavior in your code.

Therefore, it is essential to design class hierarchies and inheritance relationships carefully.

Ensuring that derived classes maintain a strong "is-a" relationship with their base classes and exhibit compatible behavior.

Note. Example of Liskov Substitution Principle (LSP) in C# with a simple example involving geometric shapes.

We'll use polymorphism to demonstrate the principle.

1. We have a base class Shape with a virtual method Area() that returns 0 as a default for an unknown shape.

using System;

// Base class for geometric shapes
class Shape
{
    public virtual double Area()
    {
        return 0.0; // Default implementation for an unknown shape
    }
}

2. Create a derived class Circle from the base class Shape.

Ensuring that the derived classes maintain the "is-a" relationship and behavioral compatibility with the base class.

class Circle : Shape
{
    public double Radius { get; set; }

    public Circle(double radius)
    {
        Radius = radius;
    }

    public override double Area()
    {
        return Math.PI * Math.Pow(Radius, 2);
    }
}

3. We create two derived classes, Circle and Rectangle, representing specific shapes. Each derived class overrides the Area() method to provide the appropriate area calculation.

// Derived class Rectangle
class Rectangle : Shape
{
    public double Width { get; set; }
    public double Height { get; set; }

    public Rectangle(double width, double height)
    {
        Width = width;
        Height = height;
    }

    public override double Area()
    {
        return Width * Height;
    }
}

4. In the Main method, we create instances of Circle and Rectangle and pass them to a method CalculateAndDisplayArea(), which accepts a Shape parameter.

class Program
{
    static void CalculateAndDisplayArea(Shape shape)
    {
        double area = shape.Area();
        Console.WriteLine($"Area: {area}");
    }

    static void Main()
    {
        Shape shape1 = new Circle(5);
        Shape shape2 = new Rectangle(4, 6);

        CalculateAndDisplayArea(shape1); // Calculate and display the area of a circle
        CalculateAndDisplayArea(shape2); // Calculate and display the area of a rectangle
    }
}

5. The Liskov Substitution Principle is demonstrated here because instances of derived classes (Circle and Rectangle) can be used interchangeably with instances of the base class (Shape).

This is because they maintain the "is-a" relationship (e.g., a Circle is a Shape) and exhibit compatible behavior through Polymorphism.


Similar Articles