Making a class sealed means that the compiler knows the complete list of implementation classes at compile time. Therefore, it can confirm that (for example) every possible match has been handled.

Consider it similar to checked exceptions: At compile time, the compiler ensures that every checked exception that could be thrown is handled somehow (either by catching or by a throws clause), so it's guaranteed that the flow control will be formally consistent.

Answer from chrylis -cautiouslyoptimistic- on Stack Overflow
🌐
freeCodeCamp
freecodecamp.org › news › write-better-java-code-pattern-matching-sealed-classes
How to Write Better Java Code using Pattern Matching and Sealed Classes
January 20, 2026 - Pattern guards can be used within cases to further enhance code readability and maintainability. Sealed classes allow developers to restrict the set of classes that can extend or implement a given class or interface.
🌐
Neil Madden
neilmadden.blog › 2026 › 04 › 24 › java-sealed-classes-and-exhaustive-pattern-matching
Java sealed classes and exhaustive pattern matching – Neil Madden
April 24, 2026 - Java 17 introduced sealed classes, which allow you to explicitly list the allowed sub-types of an interface or base class. For example, here’s a toy example using a sealed interface and records (inner classes are implicitly added to the permitted sub-types if an explicit list is not given): public sealed interface SealedType { record TypeA()…
Discussions

Java 17 Features: Pattern Matching for switch and Sealed Classes
Wow. Java getting features from Scala. The pattern matching is sweet. More on reddit.com
🌐 r/programming
21
18
September 27, 2021
class - What are sealed classes in Java 17? - Stack Overflow
Sealed classes is an addition to the Java language giving a class author a fine-grained control over which classes can extend it. Before, you could either allow everyone to inherit your class or disallow it completely (using "final"). It also works for interfaces. Additionally, it's a prerequisite for the pattern matching ... More on stackoverflow.com
🌐 stackoverflow.com
Sealed class in Java
They're very useful for modelling sum types (aka disjoint union types). Using sealed types then allows exhaustiveness checks when using pattern matching over the type. You can achieve the same without sealed types but it's much more verbose and brittle. More on reddit.com
🌐 r/java
27
29
September 6, 2023
JEP 441: Pattern Matching for switch. Formally Proposed to Target JDK 21
After this, there's not much work left to make Java a fully modern language. Between lambdas, streams, type inference, record classes, sealed classes, switch expressions, and pattern matching, Java 21 will basically be unrecognisable to a Java 7 developer, and that's a good thing. More on reddit.com
🌐 r/java
92
142
May 4, 2023
🌐
Medium
medium.com › @oskarv › java-17-features-pattern-matching-for-switch-and-sealed-classes-f631bdd56f12
Java 17 Features: Pattern Matching for switch and Sealed Classes | by Oskar | Medium
September 26, 2021 - Sealed classes work really well with records, caveat is that records are implicitly final , so by design your sub class (sub record) modifier options are reduced from final , sealed or non-sealed to, well, only final · As mentioned in the beginning, this feature is designed to work hand in hand with pattern matching workflows.
🌐
TheServerSide
theserverside.com › tip › Use-sealed-classes-in-Java-to-control-your-inheritance
Java sealed classes: permits, non-sealed and pattern matching
3 weeks ago - For example, the following class fails to compile because DivideExpr is not a permitted implementation of Expr: ... The compiler rejects the declaration because DivideExpr is not listed as a permitted subtype. One of the most useful modern applications of sealed types is exhaustive pattern matching.
🌐
Foojay
foojay.io › home › sealed interfaces and pattern matching: a quick dive into java’s modern capabilities
Sealed Interfaces & Pattern Matching: Java's Modern Capabilities
August 9, 2023 - Sealed classes in Java are a new feature that provides a way to restrict the classes that can inherit from a superclass or extend an interface. This new language feature enhances the encapsulation and provides more control to developers over their codebase. In this tutorial, we will explore sealed classes, how to use them to find all subclasses, and how to apply pattern matching in a practical context.
🌐
Colin.java
colinchjava.github.io › 2023-10-24 › 08-59-06-422344-pattern-matching-for-sealed-classes-in-java-20
Pattern matching for sealed classes in Java 20
October 24, 2023 - Pattern matching for sealed classes ... the handling of different subclasses, pattern matching enhances code readability and maintainability, while enforcing encapsulation....
Find elsewhere
🌐
Medium
medium.com › @shaikreshma21082000 › java-17-pattern-matching-sealed-classes-and-whats-next-e92f6f57bdb3
Java 17: Pattern Matching, Sealed Classes, and What’s Next? | by shaik reshma | Medium
November 3, 2024 - Sealed classes in Java restrict which classes can extend or implement them, allowing developers to define a fixed set of subclasses. This promotes type safety and exhaustiveness in pattern matching, ensuring that all possible cases are handled.
🌐
Medium
medium.com › @sch-codecs › sealed-classes-pattern-matching-and-spring-boot-a-powerful-trio-7cb6dadde41d
Sealed Classes, Pattern Matching, and Spring Boot: A Powerful Trio | by S C | Medium
December 29, 2024 - Sealed classes and pattern matching can be integrated into Spring Boot applications to model domain objects and handle request or response transformations effectively. Let’s build a simple Spring Boot application to manage shapes.
🌐
Foojay
foojay.io › home › java sealed classes in action: building robust and secure applications
Java Sealed Classes: Building Robust and Secure Applications
March 2, 2023 - By limiting the set of subtypes that can extend a sealed class, developers can use pattern matching with exhaustive checks, ensuring that all possible subtypes are covered.
🌐
Foojay
foojay.io › home › embracing modernity: a comprehensive look at sealed classes, pattern matching, and functional paradigms in java
A Look at Sealed Classes, Pattern Matching, Functional Paradigms
September 11, 2023 - Immutable Hierarchies: Sealed Classes often lead to hierarchies where instances are immutable. Pattern Exhaustiveness: By defining a fixed set of permitted subclasses, Sealed Classes ensure that all possible cases are handled.
🌐
OpenJDK
openjdk.org › jeps › 409
JEP 409: Sealed Classes
January 27, 2021 - A significant benefit of sealed ... will be able to use a switch enhanced with patterns. The use of sealed classes will allow the Java compiler to check that the patterns are exhaustive....
🌐
Medium
medium.com › @masha.komarkofffa › mastering-sealed-classes-switch-pattern-matching-in-java-write-safer-smarter-code-8700e69a3f6f
🚀 Mastering Sealed Classes & Switch Pattern Matching in Java: Write Safer, Smarter Code! | by Mariia Komarkova | Medium
April 1, 2025 - Java’s switch pattern matching allows for cleaner, more readable, and safer handling of different types at runtime. Instead of using traditional switch statements with constants, or manually checking and casting, pattern matching directly ...
🌐
Reddit
reddit.com › r/programming › java 17 features: pattern matching for switch and sealed classes
r/programming on Reddit: Java 17 Features: Pattern Matching for switch and Sealed Classes
September 27, 2021 - java.time classes). As for adding null to switch, it is exactly due to providing additional null safety. This way it remains backwards-compatible and will work in the new “pattern-matchy” way when it encounters a null.
🌐
Java Code Geeks
javacodegeeks.com › home › core java
Sealed Classes and Exhaustive Pattern Matching: How They Change API Design, Not Just Syntax - Java Code Geeks
April 3, 2026 - Pattern matching lets you refine a case with an arbitrary boolean predicate — giving you the expressiveness of a full conditional tree inside a tightly typed switch expression. You’re not choosing between type safety and nuance; you get both.
🌐
Baeldung
baeldung.com › home › java › core java › sealed classes and interfaces in java
Sealed Classes and Interfaces in Java | Baeldung
December 11, 2025 - In this article, we explored sealed classes and interfaces, a new feature in Java SE 17. We covered the creation and usage of sealed classes and interfaces, as well as their constraints and compatibility with other language features. In the examples, we covered the creation of a sealed interface and a sealed class, the usage of the sealed class (with and without pattern matching), and the compatibility of sealed classes with records and the reflection API.
🌐
Medium
medium.com › @kumar_sachin › understanding-sealed-classes-in-java-17-b542550b7d5e
Understanding Sealed Classes in Java 17 | by Sachin Kumar | Medium
February 16, 2024 - Additionally, sealed classes work best when used in conjunction with switch expressions and pattern matching to ensure type safety and readability. In conclusion, sealed classes in Java 17 offer a powerful mechanism for controlling inheritance ...
🌐
Expertbeacon
expertbeacon.com › home › how to write better java code using pattern matching and sealed classes: an expert guide
How To Write Better Java Code Using Pattern Matching And Sealed Classes: An Expert Guide - ExpertBeacon
September 10, 2024 - By extracting imperative logic into match services, we can unit test classes leveraging pattern matching by mocking. From analyzing industry surveys on Java language feature adoption in 2022, pattern matching usage grew rapidly since the introduction of the feature in Java 16. Over 40% of surveyed developers now utilize pattern matching in projects. However, sealed classes adoption trails at only 13% likely due to being a more recent Java 17 addition.
Top answer
1 of 10
67

You can follow this link for examples.

In short, sealed classes gives you the control of which models, classes etc. that can implement or extend that class/interface.

Example from the link:

public sealed interface Service permits Car, Truck {

    int getMaxServiceIntervalInMonths();

    default int getMaxDistanceBetweenServicesInKilometers() {
        return 100000;
    }
}

This interface only permits Car and Truck to implement it.

2 of 10
38

The JEP 409 explains it as

A sealed class or interface can be extended or implemented only by those classes and interfaces permitted to do so.

A more practical explanation is the following:

The situation in the past was:

  • You could not restrict an interface being extended by another interface
  • You could not constraint which classes where able to implement a specific interface.
  • You had to declare a class as final in order to not be extended by another class. This way no class could extend the declared final class. This was black or white approach.

The current situation with sealed keyword is:

  • You can now restrict an interface being extended by other interfaces and make a rule for only some specific interfaces which will be allowed to extend it.

    Example:

    public sealed interface MotherInterface permits ChildInterfacePermitted {}
    
    //Has to be declared either as sealed or non-sealed
    public non-sealed interface ChildInterfacePermitted extends MotherInterface {}  
    
    public interface AnotherChildInterface extends MotherInterface {} 
    //compiler error! It is not included in the permits of mother inteface
    
  • You can now create an interface and select only specific classes that are allowed to implement that interface. All other classes are not allowed to implement it.

    Example:

     public sealed interface MotherInterface permits ImplementationClass1 {} 
    
     //Has to be declared either as final or as sealed or as non-sealed
     public final class ImplementationClass1 implements MotherInterface {} 
    
     public class ImplementationClass2 implements MotherInterface {} 
     //compiler error! It is not included in the permits of mother inteface
    
  • You can now restrict a class being extended (same as before with final) but you can now allow some specific classes to extend it. So now you have more control as before the keyword final was absolute restricting every class from extending the declared final class

    Example:

    public sealed class MotherClass permits ChildClass1 {}
    
    //Has to be declared either as final or as sealed or as non-sealed
    public non-sealed class ChildClass1 extends MotherClass {} 
    
     public class ChildClass2 extends MotherClass {} 
     //compiler error! It is not included in the permits of MotherClass
    

Important notes:

  • The sealed class and its permitted subclasses must belong to the same module, and, if declared in an unnamed module, to the same package.

    Example:

    Let's say that we have the same unnamed module and the following packages

      -packageA
         -Implementationclass1.java
      -packageB
         -MotherClass.java
    

    or

       -root
          -MotherClass.java
          -packageA
             -Implementationclass1.java
    

    You will get the error Class is not allowed to extend sealed class from another package. So if you have an unnamed module all participating classes and interfaces for the sealed function must be placed exactly on the same package.

  • Every permitted subclass must directly extend the sealed class.