DEV Community

Anh Trần Tuấn
Anh Trần Tuấn

Posted on • Originally published at tuanh.net on

Reasons to Embrace the Liskov Substitution Principle (LSP) in Object-Oriented Design

1. What is the Liskov Substitution Principle (LSP)?

Image

The Liskov Substitution Principle was introduced by Barbara Liskov in 1987 and forms the "L" in SOLID principles. LSP states that objects of a superclass should be replaceable with objects of a subclass without affecting the correctness of the program. In simpler terms, if class B is a subclass of class A, then objects of class A should be interchangeable with objects of class B without altering the desirable properties of the program.

1.1 Formal Definition of LSP

LSP can be formally defined as:

If S is a subtype of T, then objects of type T may be replaced with objects of type S without altering the correctness of the program.

This principle enforces that a subclass should enhance, rather than diminish, the behavior of the parent class.

1.2 Why LSP is Crucial in OOP

LSP ensures that a class hierarchy remains logical and that the inherited behavior remains consistent across subclasses. Violating LSP can lead to fragile code that is difficult to maintain and extend, ultimately increasing the risk of bugs.

1.3 Example of Violating LSP

Let’s consider a classic example involving rectangles and squares.

Image

class Rectangle {
    protected int width;
    protected int height;

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

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

    public int getArea() {
        return this.width * this.height;
    }
}

class Square extends Rectangle {
    @Override
    public void setWidth(int width) {
        this.width = width;
        this.height = width;
    }

    @Override
    public void setHeight(int height) {
        this.width = height;
        this.height = height;
    }
}
Enter fullscreen mode Exit fullscreen mode

In this example, a Square is a specific type of Rectangle where the width and height must always be equal. However, by overriding the setWidth() and setHeight() methods, the square changes the behavior expected from a rectangle. This violates the Liskov Substitution Principle because if you substitute a Rectangle with a Square , the behavior of the program could change unexpectedly.

1.4 Fixing the Violation: Refactoring the Design

To adhere to LSP, the Square class should not inherit from Rectangle. Instead, we can refactor the design to better reflect the relationship:

Image

interface Shape {
    int getArea();
}

class Rectangle implements Shape {
    protected int width;
    protected int height;

    public Rectangle(int width, int height) {
        this.width = width;
        this.height = height;
    }

    @Override
    public int getArea() {
        return this.width * this.height;
    }
}

class Square implements Shape {
    private int side;

    public Square(int side) {
        this.side = side;
    }

    @Override
    public int getArea() {
        return this.side * this.side;
    }
}
Enter fullscreen mode Exit fullscreen mode

By separating the Rectangle and Square classes and implementing a common Shape interface, we now respect the Liskov Substitution Principle. Both Rectangle and Square can be used interchangeably through the Shape interface without any unexpected behavior.

2. Practical Applications of LSP

One of the primary benefits of adhering to LSP is that it allows for more flexible and reusable code. When classes in a hierarchy can be substituted freely, it becomes easier to extend the system with new features without breaking existing functionality.

LSP also improves testability. If you can replace one class with another in your tests without altering the behavior, it becomes easier to create unit tests and mock dependencies. This leads to better test coverage and more reliable code.

Following LSP often leads to better adherence to the Interface Segregation Principle (ISP). By designing classes that respect LSP, you’re more likely to create interfaces that are specific to the needs of the subclasses, which in turn reduces the likelihood of creating bloated or unnecessary methods in your interfaces.

2.4 Avoiding Design Pitfalls

Ignoring LSP can result in design pitfalls such as:

  • Tight coupling : Subclasses that override or alter base class methods too much can introduce tight coupling, making the code harder to modify or extend.
  • Unexpected behavior : When subclasses do not behave as expected, it can lead to bugs that are hard to trace and fix.

3. Conclusion

Understanding and applying the Liskov Substitution Principle is key to building robust, maintainable, and scalable object-oriented systems. By ensuring that subclasses can be used interchangeably with their base classes without altering the program's behavior, you promote flexibility, testability, and adherence to good design principles.

If you have any questions about LSP or how to apply it in your projects, feel free to comment below!

Read posts more at : Reasons to Embrace the Liskov Substitution Principle (LSP) in Object-Oriented Design

Top comments (0)