Stop 05 · Inheritance, Polymorphism & InterfacesTopics · 7/9

Sealed Classes

Part of the Java Roadmap

In short

Some sets of types should never grow by accident. Sealed classes let you say "these are the only payment types", and the compiler then checks your switch covers every one.

Sealed classes and interfaces (Java 17) list exactly which classes may extend them using permits. This lets you model a fixed set of types, such as payment methods, and lets switch check that every case is handled.

The jaadu, dressed as a ticket checker, stands on a railway platform beside a train whose few coach doors are all firmly marked and locked except the listed ones.

Think of it like this

Fixed coach classes on a train

An Indian Railways train has a fixed list of classes: Sleeper, 3AC, 2AC, 1AC. A passenger can't invent a new "4AC" class and board. Because the list is fixed, the ticket checker knows exactly which cases exist. A sealed type is that fixed list, written into your code.

Watch it happen

A sealed Payment hierarchy

sealed Payment
Upi
Card
CashOnDelivery

1/3Payment is sealed and permits exactly three subtypes.

The big ideas

What you need to know

sealed + permits

sealed interface Payment permits Upi, Card, CashOnDelivery lists every allowed subtype. Any other class trying to implement it fails to compile.

Subtypes must choose

Each permitted subclass must be declared final, sealed (with its own list) or non-sealed (open again).

Exhaustive switch

Pattern-matching switch (Java 21) knows all cases, so you skip default and the compiler flags any missing case when a new type is added.

Pairs well with records

Records are implicitly final, which makes sealed interface plus records a neat way to model fixed data shapes.

See it in code

Walk through the code

java
1sealed interface Payment permits Upi, Card, CashOnDelivery {}2 3record Upi(String upiId) implements Payment {}4 5record Card(String network, String last4) implements Payment {}6 7record CashOnDelivery() implements Payment {}8 9final class PaymentText {10    static String describe(Payment p) {11        return switch (p) {12            case Upi u -> "UPI from " + u.upiId();13            case Card c -> c.network() + " card ending " + c.last4();14            case CashOnDelivery cod -> "Pay cash at the door";15        };16    }17}

1/3The full list of allowed subtypes, fixed at compile time. Sealed classes are standard since Java 17.

PaymentText.describe(new Card("RuPay", "4821")) returns "RuPay card ending 4821". Needs Java 21 for pattern-matching switch.

Save yourself the pain

Common mistakes

Oops

Leaving a permitted subclass as a plain class with no modifier.

Do this instead

Each permitted subclass must say final, sealed or non-sealed, otherwise it's a compile error.

Oops

Adding default -> "unknown" to every switch over a sealed type.

Do this instead

A default hides missing cases. Leave it out so the compiler warns you when a new subtype appears.

Oops

Putting permitted subclasses in a different package of an unnamed module.

Do this instead

Without modules, the sealed type and its subclasses must be in the same package.

Your mission

Model order status as a sealed type

~25 min

Quick check

What must a class in the permits list of a sealed class be declared as?

Go deeper

  1. 1Create a sealed interface Payment that permits UpiPayment, CardPayment and CashPayment.
  2. 2Try to add an unlisted subclass and read the compiler error.
  3. 3Combine the sealed type with a switch expression that has no default branch.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge