Liskov Substitution Principle
The application should work correctly when we put in the base class place every derived class. There is required full interface and method compatibility. You should remember, during designing class hierarchy, that every class can use their method from base class without overriding them. Methods of derivative classes should at most extend base method (can call them in their implementation).
This rule can be applied to methods, functions, classes, modules and packages.
- More readable code.
- More understandable code.
- Fewer surprises in code.
- Easier maintenance.
- Possibility to substitute base class by any derivative class with sure, that the program works fine.
- Fewer dependencies in code — handling list of objects do not require checking of this object type and handling in a specific way.
- The need to think better about class structure and inheritance.
- Square inherits from Rectangle — AreaCount needs to be overridden in Square class.
- Fish inherits from Animal, where Animal has method Run. Fish do not run of course!
- Classes FileReadonly and FileWriteAndRead inherit from File. Class File has method Read and Write. In FileReadonly method Read is… confusing.
All posts from mini project: Learn SOLID and OOP principles:
- SOLID exercises
- S like Single responsibility principle
- SOLID exercises — Kata
- O as Open-closed principle
- L jak Liskov Substitution Principle
- Interface segregation principle
- KISS — Keep it simple, stupid
- DRY — Don’t repeat yourself
- Dependency inversion principle
- SLAP — Single Level of Abstraction Principle
- Composition Over Inheritance
- Encapsulate what changes
- Lod — Law of Demeter
- ES as Exercises of Single responsibility principle
- EO as Exercises of Open/closed principle
- EL as Exercises of Liskov Substitution Principle
- EI as Eexrcises of Interface segregation principle
- ES as Exercises of Dependency Inversion Principle
- Object-oriented programming
- OOP — Object-Oriented Programming — Advice
- OOP — Object Oriented Programming
- Clean architecture — Robert C. Martin
- The Liskov Substitution Principle – Robert C. Martin – https://drive.google.com/file/d/0BwhCYaYDn8EgNzAzZjA5ZmItNjU3NS00MzQ5LTkwYjMtMDJhNDU5ZTM0MTlh/view