Pro EP 61: Liskov Substitution Principle of SOLID
LSP is third principle of SOLID , it is extension of open/close principle.
LSP says :-
- Derived class must be correctly substitutable for base class
- Subtypes must be substitutable for their base type (interface, class)
- Sub class should be able to replace objects of base class without affecting
LSP tell us how to use inheritance properly , if you see somewhere in your code NotImplementedException
then it is most probably violation of LSP.
Benefits of LSP:-
- Code reusability
- More maintainable code
- Better use of inheritance
Example
Bad:- A base class with some methods and sub classes throwing NotImplementedException for those methods it don’t need
Good:- A base class with some methods and sub classes implement those methods and those are needed as well
Whenever you’re ready, there are 4 ways I can help you
- Subscribe to my Weekly .NET Newsletter of C#/.NET with 7000+ Software Engineers.
- Promote yourself to 7000+ subscribers by Sponsoring my Newsletter
- Download my eBook at Gum Road which contains 30+ .NET Tips (With 2800+ Downloads)
- TTo get 100+ free and paid resources for learning C# and .NET visit this GitHub Repository