Sub topic 1
Why You Might Want a Closed Hierarchy
A type hierarchy is a set of related types connected by inheritance or interface implementation. An open hierarchy allows code outside the original design to add new subtypes. An abstract class or ordinary interface usually creates an open hierarchy.
A closed hierarchy is one whose complete set of subtypes is known and controlled by its author. Java sealed classes and interfaces provide this control. They are useful when a value must be one of a fixed set of alternatives.
Prerequisites
This chapter assumes that you understand Java classes, interfaces, inheritance, the final keyword, records, and basic switch expressions. You should also know that an interface can have many implementations, including implementations created in another package or library.
The problem with open hierarchies
Suppose an application supports three payment methods:
public class Main {
interface PaymentMethod {}
record Card(String number) implements PaymentMethod {}
record BankTransfer(String account) implements PaymentMethod {}
static int processingFee(PaymentMethod method) {
if (method instanceof Card) {
return 2;
}
if (method instanceof BankTransfer) {
return 1;
}
// The method appears to handle all known payment methods.
return 0;
}
public static void main(String[] args) {
PaymentMethod method = new Card("1234");
System.out.println(processingFee(method));
}
}The method works for the two implementations known when it was written. However, another developer can add this implementation:
record Cryptocurrency(String wallet) implements Main.PaymentMethod {}The compiler does not require processingFee to change. The new type reaches the fallback and receives a fee of 0. That result might be wrong, and the mistake can remain hidden until production.
This is the central problem: the compiler cannot tell whether the programmer has handled every meaningful variant.
Why close the hierarchy?
A closed hierarchy is a good fit when:
- The alternatives represent a complete business rule.
- Each alternative needs different behavior.
- Missing a case should be a compilation error.
- You own the type and want to protect its invariants.
- New variants should require deliberate changes to existing code.
Examples include the states of a network request, the results of a parser, or the shapes supported by a drawing program.
The following diagram shows the difference in extension control:
Closed does not mean immutable
Sealing controls who may be a subtype. It does not make objects immutable, prevent methods from changing state, or freeze the source code forever. You can still evolve a sealed hierarchy, but adding a new permitted variant may require updates wherever code handles all variants.
This compiler-visible knowledge becomes especially valuable with pattern matching. In Java 21, a switch over a sealed hierarchy can be checked for exhaustiveness. If every permitted subtype is handled, the switch can omit a default branch. When a new subtype is added, affected switches can fail to compile until they handle the new case.
That behavior turns an accidental omission into an explicit maintenance task.
Sealed types versus other choices
| Design | Who can add subtypes? | Best use |
|---|---|---|
| Enum | Only the enum declaration defines constants | A fixed set of simple named values |
| Sealed type | Only permitted types can extend or implement it | A fixed set of related data variants |
| Abstract class | Generally any accessible subclass | Shared state or behavior with controlled conventions |
| Open interface | Any accessible implementation | Extension points and plug-in APIs |
Enums are simpler when every alternative is just a name. Sealed types are more expressive when each variant has different fields or behavior.
Exercise
What problem does a closed hierarchy solve that an ordinary interface does not?
Show answer
It lets the compiler and API author know the complete set of permitted variants, so missing handling can be detected instead of silently falling through a fallback case.