Skip to content
Ameen Abozaid
In reviewJNR-001-L03

Sealed classes and interfaces

  • Lesson 3 of 8
  • 11 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

model a closed set of types.

Planned topics

  • sealed lists exactly which classes may implement an interface.
  • Like a crate label: no other kind of crate can exist.
  • Use permits to name the allowed types.
  • Each allowed type must be final, sealed, or non-sealed. Records are final already.
  • The compiler now knows every case, which helps switch.
Draft outline details

Demonstration

java
sealed interface Discount permits Percentage, FixedAmount, BuyXGetY {}
record Percentage(int percent) implements Discount {}
record FixedAmount(Money amount) implements Discount {}
record BuyXGetY(int buy, int free) implements Discount {}

Check your understanding

What does permits do?

Show answer

It names the only types allowed to implement the sealed interface.

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.