Sub topic 10
What Sealed Types Do Not Allow
This chapter assumes you understand Java classes, interfaces, inheritance, packages, modules, records, enums, and the final keyword. It also assumes you can read switch expressions. The focus here is on the restrictions that make a sealed hierarchy closed and predictable.
A sealed type rejects unknown direct subtypes
A sealed class or interface lists its permitted direct subtypes. A class outside that list cannot extend or implement the sealed type.
sealed interface Command permits Start, Stop, Batch {
}
record Start() implements Command {
}
final class Stop implements Command {
}
non-sealed class Batch implements Command {
}
class SpecialBatch extends Batch {
}
public class Main {
public static void main(String[] args) {
Command first = new Start();
Command second = new SpecialBatch();
System.out.println(first.getClass().getSimpleName());
System.out.println(second.getClass().getSimpleName());
}
}SpecialBatch is allowed because it extends Batch, not Command directly. However, this declaration would fail to compile:
class Audit implements Command {
}Audit is not listed in the permits clause. Sealing controls direct inheritance, so every direct subtype must be known when the sealed type is compiled.
Every permitted subtype needs a valid modifier
A permitted direct class must choose one of three forms:
finalends inheritance at that class.sealedcontinues the controlled hierarchy and supplies anotherpermitsclause.non-sealedremoves sealing below that class.
A permitted record is implicitly final, so it satisfies this rule. An enum that implements a sealed interface is also effectively unable to have subclasses.
| Declaration | What it allows |
|---|---|
| `final class Stop` | No class can extend `Stop` |
| `sealed class Batch permits SpecialBatch` | Only listed classes can extend `Batch` |
| `non-sealed class Batch` | Any accessible class can extend `Batch` |
A direct subtype cannot omit this choice. For example, this is invalid:
sealed interface Event permits Login {
}
class Login implements Event {
}Login must be declared as a record, or as final, sealed, or non-sealed.
A non-sealed modifier is meaningful only for a direct subtype of a sealed type. You cannot use it on an unrelated class merely to make that class open.
Permitted types must be in the right place
A permitted direct subtype must be declared in the same Java module as its sealed parent. If the code is not in a named module, the parent and direct subtypes must be in the same package.
The subtype must also be a direct subtype. This does not work:
sealed interface Message permits ImportantMessage {
}
final class ImportantMessageBase implements Message {
}
final class ImportantMessage extends ImportantMessageBase {
}The declaration is invalid because ImportantMessage is not a direct subtype of Message, while ImportantMessageBase is the actual direct subtype and is not listed.
When the permitted types are declared in the same compilation unit, Java can infer the list, so permits may be omitted:
sealed interface Result {
record Success(String value) implements Result {
}
record Failure(String reason) implements Result {
}
}The nested records are the only direct implementations in this compilation unit. If you later add another direct implementation elsewhere, the declaration must be changed to list it explicitly, and the new type must obey the same package or module rule.
What sealing does not control
Sealing controls inheritance, not object creation or object state. It does not:
- prevent callers from constructing permitted classes;
- make fields immutable;
- replace access control such as
privateorprotected; - stop reflection from inspecting the hierarchy;
- guarantee that every subclass has the same behavior;
- make a hierarchy safe for every thread.
A sealed interface also does not require its permitted implementations to share a superclass. They may be classes, records, or other interfaces, as long as each declaration follows the inheritance rules.
Exercise
Why is class Audit implements Command {} invalid when Command permits only Start, Stop, and Batch?
Show answer
Audit is an unlisted direct subtype of the sealed interface. It would need to be added to the permits list and declared as a valid direct subtype, or extend one of the permitted types instead.