How do you ensure subclasses follow LSP?
To ensure subclasses follow LSP:
- Subclasses should not override behavior in a way that breaks expected behavior.
- Subclasses should preserve the invariants and preconditions/postconditions of the
base class.
- Avoid overriding methods to throw exceptions for valid base class behavior.
- Use composition over inheritance if a subclass doesn’t strictly conform to the base
class behavior.
- Write unit tests to verify that the subclass behaves identically to the base class in all
valid scenarios.
✅ Ask yourself: Can this subclass be used anywhere the base class is used — without
surprises?