Sub topic 2
Declaring a Sealed Class or Interface
This chapter assumes you know Java classes, inheritance, interfaces, access modifiers, packages, records, and basic switch expressions. It also assumes you use Java 17 or later, because sealed types became a standard Java feature in Java 17.
Why seal a type?
An ordinary interface or non-final class is open. Any accessible class can implement or extend it. That flexibility is useful for frameworks and extension points, but it can make a type hierarchy difficult to reason about.
A sealed type is a class or interface that lists its direct permitted subclasses. The compiler then knows the complete set of direct variants. You can use this control to protect invariants and make later code easier to check.
The sealed modifier goes on the parent type. The permitted types appear after permits.
public class Main {
public static void main(String[] args) {
Shape shape = new Circle(2.0);
System.out.println(shape);
}
}
sealed interface Shape permits Circle, Square {
}
record Circle(double radius) implements Shape {
}
record Square(double side) implements Shape {
}Here, only Circle and Square may directly implement Shape. A declaration such as class Triangle implements Shape causes a compilation error.
Sealing a class
You can seal an abstract or concrete class. The permitted types must directly extend it.
sealed abstract class Vehicle permits Car, Truck {
}
final class Car extends Vehicle {
}
final class Truck extends Vehicle {
}The permits clause names direct subclasses, not every class somewhere below the parent. If Truck later has a subclass, that decision is made in the Truck declaration.
A sealed declaration does not make every class in the hierarchy immutable, thread-safe, or impossible to misuse. It controls inheritance. You still need to design constructors, methods, and state correctly.
The basic declaration rules
Every direct permitted subclass must choose how inheritance continues:
finalends inheritance at that class.sealedrestricts inheritance again by naming another permitted set.non-sealedreopens inheritance for that branch.
For example:
public class Main {
public static void main(String[] args) {
Vehicle vehicle = new ElectricBike();
System.out.println(vehicle.getClass().getSimpleName());
}
}
sealed abstract class Vehicle permits Car, Truck, Bike {
}
final class Car extends Vehicle {
}
sealed class Truck extends Vehicle permits PickupTruck {
}
final class PickupTruck extends Truck {
}
non-sealed class Bike extends Vehicle {
}
class ElectricBike extends Bike {
}Car cannot have subclasses. Truck may have only the permitted subclasses listed by Truck. Bike is open again, so any accessible class can extend it.
A direct permitted subclass that is neither final, sealed, nor non-sealed is invalid. This rule prevents an accidental escape from the hierarchy's declared boundaries.
The `permits` clause
The permitted types must be directly connected to the sealed type. Java also requires them to be in the same package, or in the same named module when modules are used. This keeps the hierarchy's definition within a known boundary.
You may write permitted classes in separate source files:
// Shape.java
public sealed interface Shape permits Circle, Square {
}// Circle.java
public record Circle(double radius) implements Shape {
}// Square.java
public record Square(double side) implements Shape {
}If all permitted types are declared in the same compilation unit, Java can infer the permits list:
sealed interface Token {
record NumberToken(int value) implements Token {
}
record EndToken() implements Token {
}
}The nested records are the only direct implementations of Token, so an explicit permits NumberToken, EndToken clause is unnecessary.
Exercise
What modifier should a direct subclass use when it must allow arbitrary further subclasses?
Show answer
It should use non-sealed. This reopens inheritance for that branch while keeping the subclass valid as a direct permitted subtype.