Sub topic 12
Adding a Permitted Subclass and What Breaks
Assume you know Java inheritance, records, switch expressions, and basic modules. This chapter uses Java 17 sealed types and Java 21 pattern matching for switch.
A sealed hierarchy is a closed set of allowed subtypes. Adding a subtype changes that set, so the compiler can point to code that no longer handles every possible value.
Start with a closed hierarchy
Suppose a payment system initially supports cards and cash:
sealed interface Payment permits CardPayment, CashPayment {
}
record CardPayment(String lastFourDigits) implements Payment {
}
record CashPayment() implements Payment {
}The permits clause records the complete list of direct implementations. Each permitted subtype must also follow a declaration rule. Records are implicitly final, so both records end their branches.
A pattern-matching switch can cover every payment:
static String describe(Payment payment) {
return switch (payment) {
case CardPayment card -> "Card ending in " + card.lastFourDigits();
case CashPayment cash -> "Cash payment";
};
}There is no default branch. The compiler knows that Payment has exactly these two permitted implementations.
Add the new subtype
Now the system must support bank transfers. Add the subtype to the permits clause:
sealed interface Payment permits CardPayment, CashPayment, BankTransfer {
}
record CardPayment(String lastFourDigits) implements Payment {
}
record CashPayment() implements Payment {
}
record BankTransfer(String accountId) implements Payment {
}The new record is legal because it directly implements Payment and is implicitly final.
The original describe method must now be updated:
static String describe(Payment payment) {
return switch (payment) {
case CardPayment card -> "Card ending in " + card.lastFourDigits();
case CashPayment cash -> "Cash payment";
case BankTransfer transfer -> "Bank transfer to " + transfer.accountId();
};
}If you compile the old switch against the new hierarchy, Java reports that the switch is not exhaustive. This is useful: every source location that interprets a Payment becomes a checklist item for the new business case.
Source compatibility and binary behavior
Adding a permitted subtype is usually source-incompatible with exhaustive switches. Recompiling the application exposes missing cases at compile time.
Already compiled code needs more care. A Java 21 switch compiled as exhaustive contains runtime safeguards for a value outside the cases known when it was compiled. If a newer version of the hierarchy produces BankTransfer but an older binary still runs the old switch, the switch can fail with IncompatibleClassChangeError. It does not automatically choose a meaningful fallback.
This is one reason to update and test all consumers together. A default branch can provide compatibility behavior, but it also prevents the compiler from requiring an explicit case for every known subtype:
static String oldConsumer(Payment payment) {
return switch (payment) {
case CardPayment card -> "card";
case CashPayment cash -> "cash";
default -> "other payment";
};
}Use a default only when treating future variants uniformly is deliberate. For security, billing, or protocol code, an explicit case is often safer.
> warning > Adding a subtype is a domain change, not only a type declaration change. Search for every switch, visitor, serializer, validator, and test that interprets the sealed type.
Check declaration rules
The new subtype must be visible according to the sealed type's package or module rules. A permitted direct subtype must be in the same named module as its sealed parent. Without named modules, it must be in the same package.
The direct subtype must be final, sealed, or non-sealed:
sealed interface Payment permits BankTransfer {
}
non-sealed class BankTransfer implements Payment {
}
class ScheduledBankTransfer extends BankTransfer {
}non-sealed deliberately reopens that branch. If the new branch should remain closed, use a final class or record instead.
Exercise
Exercise
You add record CryptoPayment(String wallet) implements Payment {} to a sealed Payment hierarchy. What two places should you inspect first?
Show answer
Add CryptoPayment to the permits clause, then find every exhaustive switch or other consumer that interprets Payment and add an explicit CryptoPayment case.