This is the “L” in SOLID.
LSP suggests that IS-A relationships between classes should be replaced with IS-SUBSTITUTABLE-FOR.
In C#, the LSP is often implemented through interfaces and polymorphism.
Definition
“The Liskov Substitution Principle states that Subtypes must be substitutable for their base types.”
- Named for Barbara Liskov, who first described the principle in 1988.
In order for substitutability to work child classes must not:
- Remove base class behavior
- Violate base class invariants, these could be any constraints defined (or reasonable assumed by clients) on the base classes.
Why follow LSP?
If the classes are none substitutable then polymorphism will not work and there will be code smells added such as if conditions / switches which will be hard to maintain. This also violates the Open / Closed Principle (OCP)
You will also have issues such as NotImplementedException();
being left in inherited methods. This violates the Interface Segregation Principle (ISP)
References & Sample Code
- https://github.com/carlpaton/SOLID/blob/master/3_LSP_code_problem.md
- https://github.com/carlpaton/SOLID/blob/master/3_LSP_code_refactor.md
- https://dzone.com/articles/solid-principles-liskov-substitution-principle
Full disclosure I asked Chat GPT for this example:
1 | interface IShape { |
In this example, Rectangle and Square are both classes that implement the IShape interface. The GetArea method is defined in the interface and implemented by both classes. Now, in any part of our code, if we have a variable of type IShape, we can assign it a Rectangle or Square object without any issue.
1 | void Main() { |
Here, shape1
is assigned a Rectangle
object and shape2
is assigned a Square
object, both of them implements the IShape
interface and have a GetArea method, so it can be called without any issue and the program will work as expected.
It’s also important to notice that in this example we can not add any method in the Square
class that does not exist in the IShape
interface, otherwise it will cause a compilation error, that’s what makes it a substitute.