In simple words: the subclass should be able to substitute a base class when needed and don’t lost sense/don’t change behavior after substitution.
As inheritance allows the subclass to become polymorphic, because inheritance lets the subclass become a subtype of the base class.
Subtyping also allows subclasses to be used as substitutes for their base class. Substitution states that:
Any class, S, can be used to replace a class, B, if and only if, S is a subtype of B.
Simply put, any subclass can stand in for its base class. Because of inheritance, a subclass is expected to have the same characteristics and behave in the same way.
The base class is the more generalized class, and therefore, its attributes and behaviors should reflect it. The names given to the attributes and methods, as well as the implementation of each method must be broad enough that all subclasses can use them.
If inheritance is not used correctly, it can lead to a violation of the “Liskov Substitution Principle”. This principle uses substitution to determine whether or not inheritance has been properly used. The Liskov Substitution Principle states that:
If a class, S, is a subtype of a class, B, then S can be used to replace all instances of B without changing the behaviors of a program.
Continue reading “SOLID (L) – Liskov Substitution Principle” →