liveBook · Manning

Sealed type

As explained in Data-Oriented Programming in Java

A sealed Java type restricts which classes or interfaces may directly extend or implement it.

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 {};     #C

Inspect the compact declaration that makes a checklist status family closed and visible in one place. section 4.5

How Data-Oriented Programming in Java covers it

How deep it goes
Explains declarations, implicit permits, and OR semantics, while deferring the additional machinery behind set-based sum-type representations.
Examples
Uses checklist statuses and a Rule algebra containing Equals, And, Or, and Not.
What it assumes
Assumes Java interface and record syntax while teaching how to express OR relationships and closed alternatives.
What you can do after
Build it from scratch as a closed Java family whose permitted alternatives encode domain choices directly.
How it connects
Connects sealed interfaces with records, enums, algebraic data types, product types, sum types, and business rules.

Also explained in

Compare how 3 books explain sealed type →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Data-Oriented Programming in Java. The section links point to the parts of the book it draws on.