Liskov Substitution Principle

Liskov Substitution Principle is the third of the Software DesignSoftware Design
This page acts as a map of content for Software Design topics.

Status: #🗺️
SOLID PrinciplesSOLID Principles
SOLID principles are five design principles created to improve object-oriented [[Software Design]]. Even though its primary focus is OOP, a lot can be learned from these principles in terms of non-...
in OOP. It states this:

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

Or in plain english - if we have a class Animal and a class Dog, Dog should inherit from Animal only if we could replace any instance of Animal with an instance of Dog without breaking our code.

To do this, we need to make sure we follow a couple of guidelines when overriding Dog's methods':

  • Dog can't change any return type of any inherited method (covariance)
  • Dog can't change any input types of any inherited method (contravariance)
  • Dog can't have any additional input checks on inherited methods (cant strengthen preconditions)
  • Dog can't have any additional output checks on inherited methods (cant weaken postconditions)
  • Dog can't throw any new exceptions in inherited methods
If our Dog can't fulfil these rules, we should probably not make it inherit from Animal. See Should I be inheriting from this type Should I be inheriting from this type.

Status: #🌲