When a domain value has a known set of alternatives, an open interface leaves invalid extensions possible. A sealed Java type makes that boundary explicit: it says which classes or records may implement or extend the type, and no others. This turns “these options and no others” into a compiler-visible part of the model. section 4.5 section 4.9
Model a closed family by:
- Declaring the common type with sealed and, when needed, naming its permitted implementations with permits. section 4.5
- Representing each alternative as a record or class that implements the sealed type. section 4.5
- Keeping nested alternatives inside the interface to let Java infer the permitted set and emphasize their shared meaning. section 4.5
Checklist status
A checklist status can be exactly NotStarted, Completed, or Skipped. A declaration such as sealed interface ChecklistStatus permits NotStarted, Completed, Skipped prevents a later Blocked record from implementing the interface. The type therefore models a domain choice rather than an open hierarchy. section 4.5
Rules as a closed algebra
Sealed types can also unify business-rule data: Rule may contain Equals, And, Or, and Not. These alternatives represent equality, conjunction, disjunction, and negation, allowing larger rules to be assembled from smaller values. section 8.4
Listing 4.37 Sealing the ChecklistState type
sealed interface ChecklistStatus #A
permits NotStarted, Completed, Skipped #B
{}
record NotStarted(/*...*/) implements ChecklistStatus {}; #C
record Completed(/*...*/) implements ChecklistStatus {}; #C
record Skipped(/*...*/) implements ChecklistStatus {}; #CInspect the compact declaration that makes a checklist status family closed and visible in one place. section 4.5


