Design Patterns: S.O.L.I.D. Liskov Substitution Principle

Keeping Class Behavior Focused

As part of the S.O.L.I.D. design principles, the Liskov Substitution Principle (LSP) ensures when designing classes, the user interface must operate by contact by ensuring during run-time, each object should be replaceable by objects instantiated by a subtype of that class or vice versa.

For example, subtype classes (derived) must not throw new exceptions and users of the classes shouldn’t have to know which subtype it is using.
Derived classes should extend behavior without replacing any in parent classes.

To illustrate LSP, I’ll reuse the examples from the article Single Responsibility Principle (SRP).

I’ll begin with the Employee class.

To use this class as a template for employee of different types (Supervisor), I’ll create a new interface for this class to inherit different types of behavior as needed.

Notice when the object is instantiated, I may define it by the interface type, which may be changed to the class type – the variable type if substitutable.