secture & code

SOLID Principle: (3) Liskov Substitution Principle

Do you know the third principle of the well-known SOLID principles, the “Liskov Substitution Principle”?

Today we are going to talk about the third principle of the well-known SOLID principles, the “Liskov Substitution Principle”.“

Liskov_substitutio_principle

Liskov Substitution Principle

Barbara Liskov, a leading computer scientist, formulated the Liskov Substitution Principle (LSP) in 1987. This principle is an integral part of the SOLID principles in object-oriented programming.

What is the Liskov principle?

The principle states that if a base class has certain behaviors and properties, its derived classes must be able to inherit and use those behaviors and properties without changing the correct functioning of the program.

How to detect that we are violating Liskov's principle?

We will be violating Liskov's substitution principle when a subclass has unused methods of the parent class or methods that do not apply to the subclass we are creating.

Another case where the principle may be violated would be if a subclass redefines the method in such a way that the return type of that method changes, for example if it should return a string and the subclass redefines it to a number we would be violating the principle since that subclass would not work in all contexts.

A simpler way to realize that we violated the principle is when the tests of the base class do not work for the subclass then we will have violated it.

How to solve the violation of the principle?

To solve the violation of Liskov's substitution principle, we must ensure that the subclasses meet the same preconditions and postconditions as the base class.

Preconditions are the requirements that must be met before a method can be executed, while postconditions are the results that are guaranteed after a method has been executed.

In other words, subclasses must behave the same as the base class. This means that subclasses must:

  • Have the same type of your objects
  • Implement the same methods as the base class
  • Have the same behavior as the base class
Liskov_Substitution_Principle

Example of implementation of the Liskov substitution principle

Let's imagine that we have a base class Figure this base class will have the draw method in its class

export default class Figure {
  builder() {}
  draw(): void {
    console.log('drawing figure')
  }
}

Now we are going to have two new classes, they will be Circle and Square, as both are Shapes and will share the draw() method we can extend these classes from the base class Figure and overwrite their methods so that they draw the figure they implement.

import Figure from "./Figure"

export default class Circle extends Figure {
  draw(): void {
    console.log('drawing circle');
  }
}
import Figure from "./Figure"

export default class Square extends Figure {
  draw(): void {
    console.log('drawing square'); 
  }
}

We can now instantiate these classes and see that we can use the draw method that we have overridden

import Circle from "./Circle";
import Square from "./Square";
import Figure from "./Figure";

const figure = new Figure();
const square = new Square();
const circle = new Circle();

figure.draw();
square.draw();
circle.draw();

This will produce the following result in the console:

drawing figure
drawing square
drawing circle

As you can see we have correctly applied Liskov's substitution principle since we are using the method of the base class draw() in subclasses with the same type of expected result.

In this article, we have explored in depth the Liskov Substitution Principle, an essential component of SOLID principles in object-oriented programming. As with the principles of Single Responsibility y Open/Close, discussed in our previous posts, the Liskov Substitution Principle plays a fundamental role in the creation of efficient code.

These three principles, when applied together, not only improve code quality, but also facilitate the development of systems that are more scalable and easier to understand.

In subsequent articles, we will continue to break down the remaining principles of SOLID, thus providing a complete guide to high-quality object-oriented software development.

Frontend

Picture of Borja Ferrer

Borja Ferrer

I enjoy programming and learning more every day.
Picture of Borja Ferrer

Borja Ferrer

I enjoy programming and learning more every day.

We are HIRING!

What Can We Do