Java Reference ยท Object-Oriented Concepts
Java sealed classes and interfaces
Sealed classes and interfaces in Java 17+: permits clauses, sealed, final and non-sealed subclasses, exhaustive switch and modelling closed hierarchies.
A sealed class or interface restricts which other classes may extend or implement it. You list the permitted subtypes with permits, and the compiler (and the JVM) reject any other. Sealed types arrived as a standard feature in Java 17 and let you model a closed set of alternatives, like "a payment is a card, a transfer or cash", directly in the type system.
The payoff comes with switch: because the compiler knows every permitted subtype, a switch over a sealed type needs no default branch, and adding a new subtype later turns every incomplete switch into a compile error instead of a runtime surprise.
sealed interface Shape permits Circle, Square { } record Circle(double r) implements Shape { } record Square(double side) implements Shape { }
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.
Rules
| Rule | Detail |
|---|---|
| Permitted subclass modifiers | Each direct subclass must be exactly one of final, sealed or non-sealed. Records and enums are implicitly final. |
permits clause |
Optional if all subtypes are in the same compilation unit; the compiler infers them. Otherwise list them. |
| Location | Permitted subclasses must be in the same package (or same module for named modules) as the sealed type. |
| Direct subtype | A permitted class must directly extend or implement the sealed type. |
| Interfaces | An interface can be sealed; implementors may be classes, records or sub-interfaces. |
| Enums | enum types can implement sealed interfaces; each constant body is a hidden subclass handled for you. |
Usage notes
- Exhaustive switch: the compiler requires either all cases or a
default. Omittingdefaultis better, because new subtypes then force a compile error. - Order of cases matters for dominance: a more general case before a specific one is an error (pattern matching for switch). In the second example
Pickupmust come beforeTruck. - Records pair naturally with sealed interfaces to give algebraic data types, as in the expression example.
- Reflection:
Class.isSealed()andgetPermittedSubclasses()expose the declaration. non-sealedre-opens extension and loses exhaustiveness for that branch only; the switch still compiles becauseCustomcovers its subclasses.- Binary compatibility: the JVM checks
PermittedSubclasseswhen a class loads, so you cannot add subclasses by recompiling only some files. - Versus enums: an enum has one instance per constant; a sealed hierarchy lets each variant carry different data.
- Versus package-private constructors: sealed is explicit and checked by the compiler, and the permitted list is documented in the type.