Java 15 introduces Sealed Classes, a preview language feature, that allows classes/interfaces to restrict which other classes/interfaces may extend or implement them. Here is an example:
public sealed class Vehicle permits Car, Truck, Motorcycle { ... } final class Car extends Vehicle { ... } final class Truck extends Vehicle { ... } final class Motorcycle extends Vehicle { ... }
In the example above, Vehicle
is a sealed class, which specifies three permitted subclasses; Car
, Truck
and Motorcycle
.
The subclasses must be:
- in the same package or module as the superclass. You can even define them in the same source file as the superclass (if they are small in size), in which case the
permits
clause is not required because the compiler will infer them from the declarations in the file. - declared either
final
(i.e. cannot be extended further),sealed
(i.e. permit further subclasses in a restricted fashion) ornon-sealed
(i.e. open for extension by any class).
Sealing serves two main purposes:
- It restricts which classes or interfaces can be a subtype of a class or interface and thus preserves the integrity of your API.
- It allows the compiler to list all the permitted subtypes of a sealed type (exhaustiveness analysis), which will (in a future Java release) enable switching over type patterns in a sealed type (and other features). For example, given the following switch statement, the compiler will detect that there is a case statement for every permitted subclass of
Vehicle
(so no default clause is needed) and it will also give an error if any of them are missing:int doSomething(Vehicle v) { return switch (v) { case Car c -> ... case Truck t -> ... case Motorcycle m -> ... }; }