Sub topic 13
Sealed Types, Enums, Abstract Classes and Open Interfaces Compared
Assume you know Java classes, inheritance, interfaces, records, enums, switch expressions, and basic generics. This chapter focuses on choosing the right modeling tool when you control, or do not control, the set of possible types.
The main design choice
Java offers several ways to represent related values:
- An enum represents a fixed set of named constants.
- An abstract class shares state or implementation while usually allowing further subclasses.
- An open interface defines a contract that any accessible class can implement.
- A sealed type defines a known set of permitted subclasses or implementations.
The important question is whether the set of variants is fixed.
When an enum is the best fit
Use an enum when every value is one of a small number of named constants, such as PENDING, PAID, or CANCELLED.
enum OrderStatus {
PENDING, PAID, CANCELLED
}
class Main {
static String message(OrderStatus status) {
return switch (status) {
case PENDING -> "Waiting for payment";
case PAID -> "Payment received";
case CANCELLED -> "Order cancelled";
};
}
public static void main(String[] args) {
System.out.println(message(OrderStatus.PAID));
}
}Enums can contain fields, methods, and constructors. However, an enum value does not naturally carry a different structure for every variant. If Paid needs a receipt number while Cancelled needs a reason, a sealed hierarchy with records usually communicates that model more clearly.
When to choose a sealed type
Use a sealed interface or class when the variants have different data or behavior, but your application needs a closed set.
sealed interface Payment
permits CardPayment, CashPayment {}
record CardPayment(String lastFourDigits) implements Payment {}
record CashPayment(int cents) implements Payment {}A sealed hierarchy documents its legal variants and lets a pattern-matching switch check that all variants are handled. This is useful for commands, results, syntax trees, and domain states.
Sealed types work especially well with records because each record can represent one immutable data variant. They also make unauthorized extension a compile-time error.
When an abstract class is better
Choose an abstract class when subclasses should share implementation, instance fields, constructors, or protected helper methods.
abstract class Report {
private final String title;
protected Report(String title) {
this.title = title;
}
public final String title() {
return title;
}
public abstract String render();
}
final class TextReport extends Report {
TextReport(String title) {
super(title);
}
@Override
public String render() {
return title() + ": text";
}
}An abstract class can be sealed if its subclasses must be controlled. Use an ordinary abstract class when extension by future code is expected.
When an open interface is better
Use an open interface when outside code should be able to add implementations. This is common for plug-ins, integrations, dependency inversion, and public library extension points.
An open interface gives you flexibility, but you cannot assume that a switch knows every implementation. Adding a new implementation in another library should not break the interface itself.
Comparing the choices
| Type model | Variants known by compiler | Can share state and implementation | Typical purpose |
|---|---|---|---|
| Enum | Yes | Yes, within enum design | Fixed named constants |
| Sealed interface | Yes | Through default methods or implementations | Closed data variants |
| Abstract class | Usually no | Yes | Shared base behavior |
| Open interface | No | Limited, through default methods | Extensible contracts |
Trade-offs and evolution
A sealed hierarchy makes exhaustive handling easier, but adding a permitted subtype requires changing the sealed declaration and reviewing every exhaustive switch. That is a useful safeguard for a closed domain, but it can be disruptive in a public API.
An enum is simpler when the variants are just labels. A sealed type is more expressive when each variant has different data. An abstract class is strongest when shared implementation matters. An open interface is safest when unknown implementations must remain possible.
Exercise
You are modeling Success(value) and Failure(message) in a library. Clients may add their own result variants later. Should you use an enum, sealed interface, abstract class, or open interface? Explain why.
Show answer
Use an open interface if clients must be able to add variants. Use a sealed interface only when the library owns and intends to control the complete set of result variants.