Sealed interfaces and records work together to model a closed set of cases as plain immutable data, which is the core of data-oriented domain modelling in modern Java. A sealed interface lists exactly which types may implement it, records give each case a compact immutable shape, and a pattern-matching switch over the sealed type is checked by the compiler for exhaustiveness. Add a new case later and every switch that forgets it stops compiling.
Records arrived as final in Java 16, sealed classes and interfaces in Java 17, and pattern matching for switch in Java 21 (an LTS release), so the combination below works on any current JDK, including Java 25.
A traditional interface is open: anyone can implement it, so the compiler cannot know all the cases. That forces a default branch or a visitor with a lot of boilerplate. A sealed interface closes the list.
sealed and permits declare the full set of subtypes.record removes the constructor, accessor, equals, hashCode and toString boilerplate for each case.This mirrors algebraic data types ("sum types" and "product types") from languages like Kotlin, Scala and Haskell.
Model a payment result as exactly three outcomes.
Records are implicitly final, which satisfies the rule that every permitted subtype must be final, sealed or non-sealed. Because the records are nested in the interface, the permits clause can even be omitted: the compiler infers the permitted subtypes from the same compilation unit. Writing it out makes the intent explicit.
There is no default. If you later add Refunded to the permits list, this method no longer compiles until you handle it. That is the main safety benefit: the compiler finds every place that needs updating.
Since Java 21 you can also deconstruct the record directly in the pattern:
A when guard refines a case. Order matters: the guarded case must come before the unguarded one for the same type, or the compiler reports it as dominated.
Records are not limited to plain data. A compact constructor lets each case enforce its own rules, so invalid states cannot be built.
Together with the sealed hierarchy, you get "make illegal states unrepresentable": a Declined result has no transaction id, an Approved result always does, and no caller needs to check for null fields that belong to another case.
Sealed records shine for trees, such as a small expression language.
eval(new Add(new Num(2), new Mul(new Num(3), new Num(4)))) returns 14. Nested record patterns are also allowed, for example Add(Num(var a), Num(var b)).
| Choice | Benefit | Cost |
|---|---|---|
| Sealed plus records | Compiler-checked cases, immutable data | Adding a case touches every switch |
| Open interface | Third parties can add implementations | No exhaustiveness checking |
| Class with behaviour methods | Logic lives next to the data | Spreads one operation across many classes |
Use sealed records for data that is stable and closed (results, events, commands, AST nodes) where many different operations are applied from outside. Use ordinary polymorphism with an open interface when you expect plug-ins or when each type carries significant behaviour of its own. Be aware that records are shallowly immutable: a record holding a List can still be changed unless you copy it with List.copyOf in the compact constructor.
Also note that serialization frameworks need support for the sealed shape. Jackson, for example, needs type information (such as @JsonTypeInfo) to decide which record to create from JSON.
Not when all permitted subtypes are in the same source file, because the compiler infers them. Otherwise the permits clause is required, and the subtypes must be in the same module (or the same package in the unnamed module).
So the compiler knows whether the hierarchy stays closed. Records are implicitly final, so they always qualify.
No. A record implicitly extends java.lang.Record and cannot extend another class, but it can implement interfaces, which is what makes sealed interfaces the natural partner.
Records need Java 16, sealed types Java 17, and record patterns with pattern-matching switch Java 21. Use a Java 21 or 25 LTS JDK for everything shown here.
A sealed interface closes the list of cases, records make each case a small immutable value, and a pattern-matching switch lets the compiler check that you handled every case. Validate inside compact constructors, copy mutable components, and use this style for stable, closed domain data such as results, events and expression trees.