Liskov Substitution
On This Lesson 4 topics
The content for this lesson is written and waiting for review. The outline below shows what it covers. It will be published once it is approved.
In this lesson
make subtypes safe to use in place of their parent.
Planned topics
- L: code that uses the parent type must work with any subtype.
- Like the socket: any plug that fits must work.
- Violation: a subclass throws
UnsupportedOperationExceptionfor an inherited method. - Violation: a subclass breaks a promise of the parent, such as "never returns null".
- Fix: change the hierarchy or use composition.
Draft outline details
Demonstration
class Penguin extends Bird { void fly() { throw new UnsupportedOperationException(); } }
// callers of Bird.fly() breakCheck your understanding
Why is throwing UnsupportedOperationException in an override a smell?
Show answer
Callers of the parent type break.
Practice
Apply this lesson with the module's lab, exercises and quiz.
Marking a lesson complete is optional and you can undo it any time. Your progress stays in this browser.