About Us Contact Us Write for Us Advertise
Home > Java > Java Sealed Classes & Pattern Matching Explained (Beginner Guide)
Java

Java Sealed Classes & Pattern Matching Explained (Beginner Guide)

Learn Java sealed classes and pattern matching: pattern matching for instanceof and switch, sealed types with permits, and how they combine for compiler-checked exhaustive switches.

Shiv Pandey
Shiv Pandey
Oct 16, 2026 | 104 views
Java Sealed Classes & Pattern Matching Explained (Beginner Guide)

Two modern Java features work beautifully together to make your code safer and cleaner: sealed classes (which control who can extend a type) and pattern matching (which lets you branch on type elegantly). They sound advanced, but the ideas are approachable — and together they eliminate a lot of clunky, error-prone code. Let's break them down.

Pattern matching: no more ugly casting

Before, checking an object's type and using it meant an instanceof check followed by a manual cast — verbose and easy to get wrong:

// the OLD way
if (obj instanceof String) {
    String s = (String) obj;   // manual cast — redundant
    System.out.println(s.length());
}

Pattern matching for instanceof binds the variable for you in one step:

// the modern way — 's' is ready to use, no cast
if (obj instanceof String s) {
    System.out.println(s.length());   // s is already a String
}

Pattern matching in switch

It gets even better with switch, letting you cleanly handle many types in one place — as an expression that returns a value:

String describe(Object obj) {
    return switch (obj) {
        case Integer i -> "number " + i;
        case String s  -> "text of length " + s.length();
        case null       -> "nothing";
        default        -> "unknown";
    };
}

Sealed classes: a controlled family of types

Sometimes you want a fixed, known set of subclasses — no surprises. A sealed class lets you declare exactly which classes are allowed to extend it, using permits:

// only Circle and Square may extend Shape — nothing else
sealed interface Shape permits Circle, Square { }

record Circle(double radius) implements Shape { }
record Square(double side) implements Shape { }

Now the compiler knows a Shape is only ever a Circle or a Square. That knowledge is powerful when combined with pattern matching.

The magic: sealed + switch = exhaustiveness

Because a sealed type has a known set of subtypes, a switch over it can be exhaustive — the compiler checks you've handled every case, and you don't even need a default:

double area(Shape shape) {
    return switch (shape) {
        case Circle c -> Math.PI * c.radius() * c.radius();
        case Square s -> s.side() * s.side();
        // no default needed — compiler knows these are ALL the cases
    };
}
Why this is a big deal: if you later add a Triangle to the permits list, the compiler will force you to handle it in every switch — turning a potential runtime bug into a compile-time error you can't miss. That's safer code, guaranteed by the compiler.

When to use these

  • Pattern matching — anytime you'd otherwise write instanceof + cast, or a chain of type checks. Use it freely; it's just cleaner.
  • Sealed classes — when you have a closed set of related types (shapes, states, event kinds, result types) and want the compiler to enforce that you handle them all.

These features clicked for me the moment I saw them together: sealed classes tell the compiler "these are all the possibilities," and exhaustive switch makes it check that I've handled every one. It's the kind of safety that catches bugs before they ship. My take: reach for pattern matching immediately — it's a pure, no-downside upgrade over instanceof + cast. Sealed classes are more situational, but when you have a fixed family of types (think a payment that's either Card, UPI, or Cash), they + exhaustive switch are genuinely elegant. Together they represent the direction modern Java is heading: safer, more expressive, less boilerplate.

Related

← What's new in Java 21 · Java cheat sheet →

Related Articles

Java Records Explained: Less Boilerplate Data Classes (Beginner Guide)
Java

Java Records Explained: Less Boilerplate Data Classes (Beginner Guide)

What’s New in Java 21? Modern Features Explained (Beginner Guide)
Java

What’s New in Java 21? Modern Features Explained (Beginner Guide)

== vs .equals() in Java: The Difference Explained (Beginner Guide)
Java

== vs .equals() in Java: The Difference Explained (Beginner Guide)

DSA in Java: Graphs — BFS & DFS Explained (Beginner Guide)
Java

DSA in Java: Graphs — BFS & DFS Explained (Beginner Guide)