Not all IS-A relationships should be modeled with Inheritance
Example: Square inherits from Rectangle
- When we perform the change width operation on both objects → Square will change its length, while Rectangle doesn’t.
- When we perform obtain length operation. Square will return its changed length, while Rectangle returns unchanged length → this violates OOP Compatibility
Fix: use a common interface
- Abstract the Square & Rectangle properties into an interface, implement the width & length in the each of the class → so we aren’t able to substitute object on the obtain length(Only available to the Reactangle object), thus ensuring OOP Compatibility
Preventing Inheritance and Method Overriding
Unit Testing
- Test cases based on expected inputs and resulted outputs
- After substituting the parent Network Object with child Network Object, we are expecting the unit test cases return the same output. Otherwise, Liskov Substitution Principle (LSP) is violated
- Example
Ensire Liskov Substitution Principle (LSP) during Compile Time
- Use of Assert