Sub topic 6
Sealed Interfaces: Closing a Type Hierarchy
Assume you know Java interfaces, inheritance, switch expressions, and basic records. You also need JDK 21 or newer for pattern matching in switch. A sealed interface is an interface that limits which classes or interfaces may directly implement it.
A normal interface is open. Any accessible class can implement it later:
interface Shape {
}A sealed interface is closed to a known set of direct subtypes. Declare those subtypes with permits:
sealed interface Shape permits Circle, Rectangle, Square {
}
record Circle(double radius) implements Shape {
}
record Rectangle(double width, double height) implements Shape {
}
record Square(double side) implements Shape {
}The permitted subtypes must be declared as final, sealed, or non-sealed. A record is implicitly final, so these record declarations satisfy the rule.
Why close a hierarchy?
Sealing gives the compiler knowledge about the complete set of direct alternatives. That knowledge is useful when a switch examines the runtime type of a value.
The following method has one case for every permitted subtype:
public class ShapeDemo {
sealed interface Shape permits Circle, Rectangle, Square {
}
record Circle(double radius) implements Shape {
}
record Rectangle(double width, double height) implements Shape {
}
record Square(double side) implements Shape {
}
static double area(Shape shape) {
return switch (shape) {
case Circle c -> Math.PI * c.radius() * c.radius();
case Rectangle r -> r.width() * r.height();
case Square s -> s.side() * s.side();
};
}
public static void main(String[] args) {
System.out.println(area(new Circle(2)));
System.out.println(area(new Rectangle(3, 4)));
System.out.println(area(new Square(5)));
}
}This switch is exhaustive. Exhaustive means that the compiler can prove every possible non-null Shape value matches a case. Therefore, the switch expression needs no default branch.
The variable after each type name is a pattern variable. For example, c has type Circle inside its case, so the code can call radius() without casting.
Controlling further inheritance
Each permitted subtype controls whether its own hierarchy stays closed:
| Declaration | Meaning |
|---|---|
| `final class A` | No class can extend `A`. |
| `sealed class A permits B` | Only listed types can extend `A`. |
| `non-sealed class A` | The hierarchy becomes open below `A`. |
For example:
sealed interface Message permits TextMessage, SystemMessage {
}
record TextMessage(String text) implements Message {
}
non-sealed class SystemMessage implements Message {
}A switch can cover TextMessage and SystemMessage, but it cannot know every future subclass of SystemMessage. If a hierarchy contains a non-sealed branch, use a default case or another suitable fallback.
Sealing is a compile-time design constraint. It does not validate objects received from an external system, and it does not prevent a permitted subtype from containing invalid state. Use constructors or validation methods for those concerns.
Exercise
Why can the area switch omit default, and what would change if Shape permitted a non-sealed subtype?
Show answer
The switch covers every permitted final subtype, so it is exhaustive. A non-sealed subtype could have unknown future subclasses, so the switch would need a fallback such as default.