Course map
Stage 1: Foundation
Stage 2: Intermediate
Stage 3: Advanced
Stage 4: DSA in Java
Stage 5: Specialization

Part 14 / Topic 1 of 7

Sealed classes and interfaces

A sealed interface or class names, with permits, every type allowed to implement or extend it. Nothing outside that list can join the hierarchy, even from the same package — the compiler enforces it at compile time, not a comment telling the next person "please don't add a fourth case here." Think of a courier's approved fleet list: a bike, a van or a truck can make a delivery; nothing else is on the list, and a new vehicle type needs to be added to the list itself before it can be used, not snuck in.

sealed interface DeliveryVehicle permits Bike, Van, Truck {
    String registrationNumber();
}

Every type named in permits must declare what happens to the hierarchy from there:

Modifier Means
final no subclass of this type can ever exist
sealed this type can be extended, but only by types it names in its own permits
non-sealed this type reopens the hierarchy — any class, anywhere, may extend it
final class Bike implements DeliveryVehicle { /* ... */ }
final class Van implements DeliveryVehicle { /* ... */ }
non-sealed class Truck implements DeliveryVehicle { /* ... */ }   // reopened on purpose

class RefrigeratedTruck extends Truck { /* ... */ }   // allowed only because Truck is non-sealed

Before pattern matching (next section), branching on the concrete type still means instanceof plus a separate cast — and the compiler does not check that an if/else chain covers every permitted type:

static String describe(DeliveryVehicle vehicle) {
    if (vehicle instanceof Bike) {
        return vehicle.registrationNumber() + ": bike, city deliveries only";
    } else if (vehicle instanceof Van) {
        Van van = (Van) vehicle;
        return van.registrationNumber() + ": van, " + van.cargoCubicMeters() + " m3 cargo";
    } else if (vehicle instanceof Truck) {
        // ...
    }
    throw new IllegalStateException("unreachable");
}
$ javac SealedTypesDemo.java
$ java SealedTypesDemo
MH12AB1234: bike, city deliveries only
MH12CD5678: van, 4.5 m3 cargo
MH12EF9012: truck, 9.0 t max load
MH12GH3456: refrigerated truck, 7.5 t max load

-- what 'sealed' actually buys you --
DeliveryVehicle permits exactly: Bike, Van, Truck
Bike and Van are final: no subclass of either can ever exist
Truck is non-sealed: RefrigeratedTruck above is allowed because of that
A fourth class trying 'implements DeliveryVehicle' elsewhere fails to compile:
  error: class Scooter is not allowed in the sealed hierarchy

A fifth vehicle class trying implements DeliveryVehicle anywhere else in the same module genuinely fails to compile, with an error naming exactly this restriction — that one line in the output above isn't a program printing a guess, it's describing what javac itself refuses to do.

🔍 Going deeper: sealed and record (section 5) are built for each other. A sealed interface permitting only records gives you a closed, exhaustively-checkable set of data shapes — the foundation sections 3 and 4 build on. enum is still the right tool when every case needs no data of its own; sealed is for when each case needs different fields.

Full program: examples/SealedTypesDemo.java.

Done here? The practice set for Part 14 — including the solutions — is on the part page.

Search the guide