Sub topic 16
Closed Hierarchies with Sealed Types and switch Patterns
So far, each shape has carried its own area() method, and Java picked the right one at runtime. This chapter shows a second way to get the same result. You list every allowed subtype up front, and then you handle each one in a single switch.
What a Sealed Type Is
A sealed type is a class or interface that names every type allowed to extend or implement it. You write sealed before it and put the list after the keyword permits. No other class can join the family.
A closed hierarchy is a family of types whose members are all known. A sealed type gives you one.
Each permitted subtype must say how it continues the family. It must be one of these:
| Modifier | Meaning |
|---|---|
| `final` | No class can extend it. |
| `sealed` | It lists its own permitted subtypes. |
| `non-sealed` | Any class may extend it again. |
A Worked Example
The program below needs Java 21 or later. Save it as Main.java, then run java Main.java.
sealed interface Shape permits Circle, Square {}
final class Circle implements Shape {
final double radius;
Circle(double radius) { this.radius = radius; }
}
final class Square implements Shape {
final double side;
Square(double side) { this.side = side; }
}
class Areas {
static double area(Shape s) {
return switch (s) {
case Circle c -> Math.PI * c.radius * c.radius;
case Square q -> q.side * q.side;
};
}
}
public class Main {
public static void main(String[] args) {
Shape[] shapes = { new Circle(1.0), new Square(2.0) };
for (Shape s : shapes) {
System.out.println(Areas.area(s));
}
}
}The output is:
3.141592653589793
4.0How the switch Works
A pattern-matching switch checks the runtime type of a value. When a case matches, it also creates a typed variable in the same step. The line case Circle c means: "if s is a Circle, call it c and run this arm."
You saw the same idea with instanceof in an earlier chapter. Here it covers many types at once.
The switch has no default branch. The compiler allows this because Shape is sealed, so Circle and Square are the only possible cases. Now suppose you add a Triangle to the permits list and forget to handle it. The switch will no longer compile, so the compiler points you to every place you need to update.
Overriding or switch: Which to Choose
Both approaches give you polymorphism. They make different changes easy.
| Approach | Easy to add | Harder to add |
|---|---|---|
| Overridden `area()` in each class | A new shape type | A new operation, because every class needs a new method |
| Sealed type with a `switch` | A new operation, written as one new method with a `switch` | A new shape type, because every `switch` needs a new case |
Use overriding when new types appear often. Use sealed types when the set of types is fixed and you keep adding operations, such as area, perimeter, and draw.
Exercise
You add final class Triangle implements Shape and add Triangle to the permits list, but you do not change Areas.area. What happens, and why?
Show answer
The code fails to compile. The switch is no longer exhaustive, meaning it does not cover every permitted subtype of the sealed Shape, and it has no default branch to catch the missing Triangle case.
Exercise
Why must Circle and Square be marked final, sealed, or non-sealed?
Show answer
Each permitted subtype must state whether the hierarchy stays closed below it. Without one of these modifiers, the compiler cannot know the full set of types, so it rejects the code.