L is for the Liskov Substitution Principle


The "L" in SOLID stands for the Liskov Substitution Principle. In my opinion, this has the most complicated definition, but the most obvious implementation, you're probably doing this without thinking about it.

This principle is named after Barbara Liskov, who defined it in 1988 as:

What is wanted here is something like the following substitution property:
If for each object o1 of type S there is an object o2 of type T such that for all
programs P defined in terms of T, the behaviour of P is unchanged when o1 is
substituted for o2 then S is a subtype of T.

Eh?

Derived classes must be substitutable for their base classes.

In my mind, this is one of the most fundamental principles of Object Oriented Programming? Definitely a staple when learning Java at University (I certainly remember this. "Dog and Cat are both types of Animal …").

Dependency injection frameworks put this into practise by supplying an implementation of the base class at run time that can just as easily be changed without any impact on the rest of your loosely coupled code.

Popular posts from this blog

I is for the Interface Segregation Principle

S is for the Single Responsibility Principle