Sub topic 5
Using Records and Enums as Permitted Subtypes
This chapter assumes you know Java interfaces, inheritance, records, enums, sealed types, and switch expressions. The examples use Java 17 syntax. Pattern-matching switch examples require Java 21 or later.
A sealed interface can permit both records and enums. This is useful when a model has two kinds of variants:
- A record represents a variant that carries data.
- An enum represents a fixed set of named values.
Records are implicitly final, so they satisfy the rule that every direct permitted subtype must be final, sealed, or non-sealed. Enum types are also not extendable, so they can be direct permitted implementations of a sealed interface.
A record and an enum in one hierarchy
public class Main {
sealed interface Shape permits Circle, Unit {
}
record Circle(double radius) implements Shape {
}
enum Unit implements Shape {
SQUARE
}
public static void main(String[] args) {
Shape measured = new Circle(2.0);
Shape named = Unit.SQUARE;
System.out.println(measured);
System.out.println(named);
}
}Circle is a record with one component, radius. Each Circle object can hold a different radius.
Unit is an enum. Its only value is SQUARE, so there is no data field for each instance in this example. The enum could have several constants:
enum Unit implements Shape {
SQUARE,
CIRCLE,
TRIANGLE
}The sealed interface allows the compiler and readers to know that every Shape is either a Circle or one of the values in Unit.
Why use an enum instead of several records?
Use an enum when the variants are a small, fixed set of named choices and do not need different instance data.
Use records when each value needs data. For example, these records describe different payment methods:
sealed interface PaymentMethod permits Card, BankTransfer, Cash {
}
record Card(String lastFourDigits) implements PaymentMethod {
}
record BankTransfer(String accountId) implements PaymentMethod {
}
enum Cash implements PaymentMethod {
INSTANCE
}Card and BankTransfer carry information. Cash needs no per-object information, so one enum constant is enough.
An enum can also contain fields and methods. The enum remains a fixed set of instances:
enum Cash implements PaymentMethod {
INSTANCE;
String description() {
return "payment made with physical money";
}
}Important restrictions
A record cannot extend an ordinary class. Every record already extends java.lang.Record, so it can only implement interfaces. This means a record is a natural permitted subtype of a sealed interface, but it usually cannot be a permitted subtype of a sealed class.
An enum also cannot extend your sealed class. Enum classes already inherit from java.lang.Enum. Therefore, enums are normally used as permitted implementations of sealed interfaces.
A record cannot be declared with final, sealed, or non-sealed. The final behavior is automatic:
record Circle(double radius) implements Shape {
}Adding final would be invalid because the record declaration already has that property.
Where the declarations can live
The record and enum must satisfy the normal location rules for permitted subtypes. They must be in the same package as the sealed type when using the unnamed module, or in the same named module. When the declarations are in the same compilation unit, the permits clause can often be omitted:
sealed interface Result {
record Success(String value) implements Result {
}
enum Failure implements Result {
INVALID_INPUT,
NOT_FOUND
}
}Here, the compiler infers Success and Failure as the permitted direct subtypes because they are declared inside the same compilation unit.
Choosing between records and enums
| Requirement | Better subtype |
|---|---|
| The variant carries values | Record |
| The variant is a fixed named choice | Enum |
| Each instance has different data | Record |
| There are only a few singleton values | Enum |
A mixed hierarchy is often clearer than forcing every variant into the same representation. Records express data, while enums express identity from a fixed list.
Exercise
Why is a record a good permitted subtype of a sealed interface, while it is usually not suitable as a subtype of a sealed class?
Show answer
A record can implement interfaces but cannot extend an ordinary class because it already extends java.lang.Record. A sealed interface therefore gives it the needed extension point.