Sub topic 13
Sealed Interfaces with Records and Enums
An enum gives you a fixed list of constants. A record gives you a simple data carrier. Sometimes you need both. You may want a type that has only a few possible forms, where some forms carry data and others do not. A sealed interface lets you describe exactly that.
What "sealed" means
A sealed interface is an interface that lists the only types allowed to implement it. You write that list after the keyword permits. If any other class tries to implement the interface, the compiler stops it with an error.
Sealed types arrived in Java 17. They answer the question "what are all the possible kinds of this thing?" and the answer is fixed in the code.
A worked example: event tickets
Suppose a concert sells tickets. A paid ticket has a holder's name and a price. A free pass has no data. It is just one of a few kinds, such as a staff pass or a press pass.
A record fits the paid ticket well. An enum fits the free passes well. A sealed interface ties the two together.
sealed interface Ticket permits Paid, FreePass {}
record Paid(String holder, int priceCents) implements Ticket {}
enum FreePass implements Ticket { STAFF, PRESS }
public class Main {
static String describe(Ticket ticket) {
if (ticket instanceof Paid p) {
return p.holder() + " paid " + p.priceCents() + " cents";
}
if (ticket instanceof FreePass f) {
return "Free pass: " + f.name();
}
throw new IllegalStateException("Unknown ticket");
}
public static void main(String[] args) {
Ticket[] tickets = { new Paid("Ana", 4500), FreePass.STAFF, FreePass.PRESS };
for (Ticket t : tickets) {
System.out.println(describe(t));
}
}
}Save this as Main.java and run it with java Main.java. The output is:
Ana paid 4500 cents
Free pass: STAFF
Free pass: PRESSThe check ticket instanceof Paid p tests the type and gives you a variable p of that type in one step.
Why records and enums fit so well
Each type that implements a sealed interface must say whether others can extend it in turn. It must be one of these:
| Modifier | Meaning |
|---|---|
| `final` | Nothing can extend it. |
| `sealed` | It lists its own permitted subtypes. |
| `non-sealed` | Anyone can extend it again. |
Records are always final. Enums are also closed to outside subclasses. So both already meet this rule, and you write no extra modifier. A regular class would need final written out, for example final class Paid implements Ticket.
What the compiler protects
Try adding this line to the file:
class Fake implements Ticket {}The compiler rejects it with an error saying that Fake is not allowed to extend the sealed interface Ticket. Because Fake is not in the permits list, nobody can sneak in a new kind of ticket.
This matters because code like describe can trust the list. It knows a Ticket is either a Paid or a FreePass. The next chapter shows how a switch can use this knowledge to check that you handled every case, so you can drop the throw line.
Exercise
Exercise
Add a third kind of ticket, a group ticket that stores a group name and a number of people. Which declarations must you add or change, and what type would you use for it?
Show answer
Declare it as a record, for example record Group(String name, int people) implements Ticket {}, because it carries data. Add Group to the permits clause: sealed interface Ticket permits Paid, FreePass, Group {}. Then add an instanceof Group g branch to describe, or tickets of the new kind will reach the throw line.