SOLID Principles — Part 3

Bob Godwin
2 min readApr 1, 2018

--

I say it’s interchangeable principle.

So the Liskov substitution principle states that all derived class should be substitutable for it’s original base class. What this means in practice is that a subclass should always be interchangeable for it’s super class. The main purpose of this principle is to guarantee semantic interoperability within the types hierarchy.

Assuming we’re required to describe the speed of a Vehicle which has a flying capability.

Vehicle is the base class and we should be able to know if any of it’s subclass can fly or not. So we could have an Airplane as a subclass deriving from Vehicle.

We also have the Car as a subclass deriving from Vehicle in order to illustrate clearly our example.

Within the application you can have different kinds of Vehicles, some of them can fly and some can’t fly. But our requirement today is to get the speed description. Therefore we could have a method that receives a “Vehicle” Type.

Liskov Substitution guarantees that our speedDescription algorithm will keep functioning regardless of which Subclass Type it receives as an argument. This term is known as semantic interoperability.

This series is covering the SOLID principles using Swift Language for practical examples, the previous path was on OPEN/CLOSE principles. For the complete playground you can find it here on the github repository.

I hope I was able to at least entertain you please don’t hesistate to reach out if you have any questions. Twitter:

--

--