Sub topic 3
final, sealed or non-sealed: what each subclass must choose
Assume you know Java inheritance, interfaces, final, records, and basic switch expressions. This chapter uses Java 17 sealed types. The pattern-matching ideas shown here also require Java 21 where noted.
A sealed type controls its direct subclasses. However, every permitted direct subclass must state what happens next. It must be one of these:
final: the subclass cannot have subclasses.sealed: the subclass restricts its own permitted subclasses.non-sealed: the subclass removes the restriction and allows normal inheritance.
This rule prevents the compiler from finding an unfinished branch in the hierarchy.
The three choices
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 {
}
public class Main {
public static void main(String[] args) {
Vehicle car = new Car();
Vehicle truck = new PickupTruck();
Vehicle bike = new ElectricBike();
System.out.println(car.getClass().getSimpleName());
System.out.println(truck.getClass().getSimpleName());
System.out.println(bike.getClass().getSimpleName());
}
}Car is final, so the branch ends at Car. No class can extend it.
Truck is sealed, so it continues the closed hierarchy. Its permits clause names PickupTruck, the only class that may directly extend it. PickupTruck is final, so that branch ends there.
Bike is non-sealed. Any accessible class can extend it, including ElectricBike. The compiler can no longer list every possible subtype below Bike.
The hierarchy therefore looks like this:
Choosing the modifier
| Modifier | Can this class be extended? | Must it list permitted subclasses? |
|---|---|---|
| `final` | No | No |
| `sealed` | Yes, only by named classes | Yes, unless the permitted types are inferred |
| `non-sealed` | Yes, without sealed restrictions | No |
Use final when the type is a complete variant. Use sealed when you need another controlled layer. Use non-sealed when you deliberately want an extension point.
Common errors
A declaration like this does not compile:
sealed class Payment permits CashPayment {
}
class CashPayment extends Payment {
}CashPayment is permitted, but it does not choose a subclass policy. Add final, sealed, or non-sealed:
final class CashPayment extends Payment {
}Another error is naming a class in permits when it does not directly extend or implement the sealed type:
sealed class Payment permits OnlinePayment {
}
final class OnlinePaymentHelper {
}OnlinePaymentHelper is not a direct subclass, so it cannot appear in permits.
Also remember that non-sealed applies only to a direct permitted subclass. You cannot use it on an unrelated class, and a non-sealed class cannot be extended by a sealed type unless the inheritance relationship allows it.
A practical decision process
For each direct subtype, ask:
- Is this the last type in this branch? Use
final. - Should this branch have its own closed set of variants? Use
sealedand addpermits. - Should external or future code extend this branch freely? Use
non-sealed. - Could callers create a subtype that your algorithms do not know about? Avoid
non-sealedunless that is intentional.
A non-sealed branch limits what exhaustive reasoning can prove. For example, a switch over Vehicle cannot assume that Car, Truck, and Bike describe every concrete object, because new subclasses of Bike may exist.
Exercise
In the vehicle example, which modifier should Motorcycle use if it is a complete variant, and which should TwoWheeledVehicle use if it has a controlled list of child types?
Show answer
Motorcycle should use final. TwoWheeledVehicle should use sealed and name its permitted direct subclasses.