Solid principles: 3. Liskov substitution principle

In my opinion, the Liskov principle is the hardest to understand, others often confuse it with other principles, let’s move on to the lesson.

What is Liskov basically about? In Wikipedia, you can find something like:

Functions that use pointers or references to base classes must also be able to use class objects that inherit from the base classes without having a thorough knowledge of these objects.

I already translate into human language 🙂

This means that any derived class can be used in a base class location, which means that it must always be 100% compatible with all methods and interfaces.

So in short:

Any derived class can be used in place of the base class (compatibility of all methods)

We break this rule, eg when the methods in the base class after which we inherit have nothing to do in the derived class that inherits from the base class, I will now show how it looks in practice in the code of our store.

In the above example, the mechanism of inheritance was wrongly planned, we created a new method in the TypeProductsInBag class, returning the amount of liters in a given product, however, the product is not always a drink, it can also be a fruit and fruits is not measured in liters and so it is breaking the Liskov principle.

Inheritance should be planned so that each derived class can use base class methods.

The principle of substitution Liskov is most often broken in cases:

  1. A poorly planned inheritance mechanism as in our example above.
  2. Application of inheritance without polymorphism.
  3. The derived classes replace the base class methods by overwriting them.

Derived classes should not override base class methods. They can possibly expand them, in C # this is done with the word base, which indicates the base class, look at the example below:

The above example follows the Liskov principle, you can use the base class method in a derived class and using it you do not overwrite it, only expand it.


You should also beware of such flowers💐:

Such a code is a sign that we inherit somewhere badly, because not all classes can be 100% substituted for the base type.


That’s all about Liskov Substitution principle.

Link to github with the whole code from this article:

This content also you can find on my steemit blog

And on medium:

As a standard, I remind you about the newsletter, which I send notifications about new entries and additional information about the IT world in general.🙂

And NECESSERILY join the DevmanCommunity community on fb, part of the community is in one place 🙂

– site on fb:ławomir Kowalski

– group on fb: DevmanCommunity

Ask, comment underneath at the end of the post, share it, rate it, whatever you want🙂.

If that post was useful for you share it with your friends :)

Post a comment

Notify about