Skip to content
Ameen Abozaid
In reviewJNR-004-L03

Liskov Substitution

  • Lesson 3 of 7
  • 10 min read
  • Junior
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 UnsupportedOperationException for 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

java
class Penguin extends Bird { void fly() { throw new UnsupportedOperationException(); } }
// callers of Bird.fly() break

Check 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.

Lab and exercises Quiz

Marking a lesson complete is optional and you can undo it any time. Your progress stays in this browser.

Continue to next lesson

Type to search levels, phases, modules and lessons.