Sealed classes and interfaces
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
sealedlists exactly which classes may implement an interface.- Like a crate label: no other kind of crate can exist.
- Use
permitsto name the allowed types. - Each allowed type must be
final,sealed, ornon-sealed. Records are final already. - The compiler now knows every case, which helps
switch.
Draft outline details
Demonstration
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.
Marking a lesson complete is optional and you can undo it any time. Your progress stays in this browser.