Sealed hierarchies
What you will learn
A sealed type restricts which classes or interfaces may directly extend or implement it. Use a sealed hierarchy to model a known family of alternatives, such as the outcomes of a fictional order review. Unlike a final class, a sealed type allows selected extensions; unlike an ordinary interface, it does not allow arbitrary direct implementations.
- Declare a sealed class or interface and identify its permitted direct subtypes.
- Choose final, sealed or non-sealed for a branch and explain its effect on future extensions.
Use a fresh folder for each complete Main.java example. Compile with javac --release 21 Main.java and run java Main. Requires JDK 21 or a compatible newer JDK. The examples use the standard library only.
Model a fixed family of outcomes
public class Main { sealed interface Review permits Accepted, Rejected {} record Accepted(int items) implements Review {} record Rejected(String reason) implements Review {} public static void main(String[] args) { Review result = new Accepted(3); System.out.println(result instanceof Accepted); System.out.println(result instanceof Rejected); }}Expected output:
truefalseReview is the common interface. Its permits clause names both direct implementations. Records are implicitly final, so these two branches cannot be subclassed. The variable is declared as Review but its current object is an Accepted.
Rules and mental model
Sealed types became a permanent Java language feature in Java 17. A direct subclass of a sealed class must close its branch with final, restrict it with sealed, or reopen it with non-sealed; records are implicitly final and enum classes have special implicit sealing/finality rules. A direct subinterface of a sealed interface must be sealed or non-sealed. In a named module, permitted direct subtypes must belong to that module; in an unnamed module, they must belong to the same package. When permits is omitted, the compiler infers named direct subtypes from the same compilation unit, not the entire classpath. Sealing limits the type hierarchy, not construction, access control, mutation, or authorization. Prefer an ordinary interface when independent third-party implementations are a requirement.
Choose where an extension is allowed
public class Main { static sealed abstract class Notice permits Receipt, CustomNotice { abstract String label(); } static final class Receipt extends Notice { String label() { return "receipt"; } } static non-sealed class CustomNotice extends Notice { String label() { return "custom"; } } static final class Reminder extends CustomNotice { @Override String label() { return "reminder"; } } public static void main(String[] args) { Notice a = new Receipt(); Notice b = new Reminder(); System.out.println(a.label()); System.out.println(b.label()); }}Expected output:
receiptreminderReceipt closes its branch. CustomNotice reopens its branch, so Reminder does not need to be named in Notice permits: it is an indirect subtype. Ordinary method overriding and dynamic dispatch still apply.
Infer the direct subtypes in one source file
public class Main { sealed interface Ticket {} record Standard(int seat) implements Ticket {} sealed interface Priority extends Ticket permits Staff {} record Staff(String department) implements Priority {} public static void main(String[] args) { Ticket t = new Staff("Support"); System.out.println(t instanceof Priority); System.out.println(t instanceof Standard); System.out.println(Ticket.class.isSealed()); }}Expected output:
truefalsetrueTicket has no explicit permits clause because its direct named subtypes are in the same compilation unit (here, Main.java). Staff is a direct subtype of Priority and an indirect subtype of Ticket. The Class API method isSealed reports whether Ticket is declared sealed.
Common mistakes
Do not list an indirect descendant in permits or assume a permitted class can omit its required modifier. For example, a plain class Guest implements a sealed interface Ticket is rejected unless it is permitted and has the required modifier. A non-sealed branch can have unknown future descendants; sealing the root does not make every branch finite.
Practice
Model a library item as a sealed interface LoanItem with two direct implementations: Book, a record holding a title, and Digital, a non-sealed class implementing LoanItem. Add Audio as a final subclass of Digital. Give LoanItem a String label() method and print labels for a Book and an Audio through LoanItem references.
Check your result with the chapter practice task and explain why it works.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Sealed hierarchies — knowledge check
Apply sealing, flow scope and pattern switch rules using Java 21-compatible examples.
What does permits list on a sealed interface?
Why can Accepted be a permitted record without final?
Can Reminder extend non-sealed CustomNotice without being named in Notice permits?
When can Ticket omit permits in the example?
Which design supports arbitrary external plug-in implementations?