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.

Answer from JDTheOne on Stack Overflow
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.

Discussions

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
Sealed classes in Java 17, and the open-closed principle - Stack Overflow
For the first time in a LTS version of Java (Java 17) we have the sealed keyword that, in a nutshell, give us the possibility to restrict the hierarchy: public abstract sealed class Person perm... More on stackoverflow.com
🌐 stackoverflow.com
Why have sealed types in Java?
I'm probably breaking a rule here but I thought this was a good question and so I went looking for an answer and found what I thought was a useful discussion on SO When and why would you seal a class? More on reddit.com
🌐 r/learnprogramming
15
13
June 27, 2024
Pattern Matching in Java 17 and Beyond
I find ADTs to be one of the most useful programming constructs. It actually how I think about problems even with Java. I think about it like a language. I think some people call this language oriented programming or something but it is kind of rare these days in non ADT languages. It is painful in Java to do it by implementing the visitor pattern and it just pisses off other developers when you add that complexity even if it is more likely to prevent bugs. One of the first programming languages I learned 20 or so years ago was OCaml. To this day it is still one of my favorite languages and its kind of why I still like Java. With the exception of its type inference OCaml is actually brutally explicit compared to other ADT languages with structural typing (e.g. Scala, Kotlin, Haskell, and even Rust). Its lack of Ad Hoc polymorphism and preference over modules (not the remotely same as Java modules) is a good thing for enterprise like development. If you are not familiar with pattern matching or ADTs check out OCaml as I think most of the modern languages that do it got inspiration from its father: ML. More on reddit.com
🌐 r/java
37
83
September 24, 2021
🌐
Oracle
docs.oracle.com › en › java › javase › 17 › language › sealed-classes-and-interfaces.html
Sealed Classes
October 20, 2025 - Sealed classes and interfaces restrict which other classes or interfaces may extend or implement them.
🌐
Medium
medium.com › codex › sealed-classes-in-java-17-8f00351d27f4
Sealed Classes in Java 17. An Introduction | by Afroz Chakure | CodeX | Medium
August 12, 2024 - Sealed Classes in Java 17 An Introduction What are Sealed Classes? Sealed classes are a feature introduced in Java 16 (as a preview feature) and finalized in Java 17. Sealed classes allow developers …
🌐
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 - In Java 17, a new feature called sealed classes was introduced. Sealed classes provide a mechanism to restrict the inheritance hierarchy for classes and interfaces.
🌐
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 ...
Find elsewhere
🌐
Medium
medium.com › @alxkm › sealed-classes-in-java-17-stronger-type-control-for-safer-code-b2de5d9253a9
Sealed Classes in Java 17+: Stronger Type Control for Safer Code | by Alex Klimenko | Medium
August 9, 2025 - One of the most powerful features introduced in Java 17 is Sealed Classes-a tool that allows developers to explicitly control which classes or interfaces can extend or implement a given type.
🌐
DEV Community
dev.to › diogomoreira › sealed-and-final-in-java-17-4221
Sealed and Final in Java (17+) - DEV Community
February 18, 2024 - The keywords sealed, non-sealed and permits are the ones to be used in the syntax of sealed classes in Java. When we mark a class as sealed, it is as if we were marking it as final, which does not allow other classes to extend it.
🌐
Medium
medium.com › @fullstacktips › sealed-classes-in-java-17-a22e4b0569e4
Sealed Classes in Java 17
February 26, 2023 - Sealed Classes are a new feature introduced in Java 17, which allow developers to restrict the inheritance hierarchy of a class or interface.
🌐
Medium
medium.com › @ujjawalr › sealed-classes-in-java-17-5bb0329ce527
Sealed classes in java 17. In this post, we will explore sealed… | by Ujjawal Rohra | Medium
July 9, 2022 - Finally, it is included as a feature in java 17 (JEP 409) . ... A sealed class allows you to restrict or choose its sub-classes.
🌐
Medium
medium.com › @mominsufyan7 › sealed-classes-in-java-17-8a580d5fa5ef
Sealed Classes in JAVA 17. Learn how Sealed Classes in Java 17… | by Mominsufiyan | Medium
October 16, 2025 - // Extended subclass from non-sealed class class EquilateralTriangle extends Triangle {} ... You explicitly define which classes can be part of your hierarchy. ... You prevent unwanted inheritance — especially useful in public APIs or frameworks. ... When combined with Java’s pattern matching and switch expressions, sealed classes enable exhaustive type checking at compile time.
🌐
javathinking
javathinking.com › blog › what-are-sealed-classes-in-java-17
Java 17 Sealed Classes: What They Are, How to Declare Them & Why They Matter (Previously a Preview in JDK 15) — javathinking.com
For example, a geometry library ... 17 introduced pattern matching for switch (JEP 406), and sealed classes enable exhaustive switch statements....
🌐
JavaTechOnline
javatechonline.com › home › sealed class in java
Sealed Class In Java With Examples
May 3, 2026 - Java has transformed remarkably over the years, introducing new features and enhancements to make the language more robust and secure. One such feature introduced in Java 15 as a preview is the ‘sealed’ keyword for classes and interfaces. It was included in Java 17 as a final feature.
🌐
OpenJDK
openjdk.org › jeps › 409
JEP 409: Sealed Classes
January 27, 2021 - Enhance the Java programming language with sealed classes and interfaces. Sealed classes and interfaces restrict which other classes or interfaces may extend or implement them. Sealed Classes were proposed by JEP 360 and delivered in JDK 15 as a preview feature.
🌐
Pandaron
pandaron.com › technology › java17-sealed-classes
Java 17: Introducing the Sealed Classes
September 28, 2023 - Sealed classes are a new type of class introduced in Java 17 as a preview feature. They provide developers with the ability to define a restricted hierarchy of classes, allowing more controlled and predictable inheritance.
🌐
DEV Community
dev.to › birajdar › java17-sealed-classes-4pl4
Java17 sealed classes - DEV Community
October 31, 2023 - Declaration of sealed class is not much complicated. simply you just need to add the sealed keyword to it's declaration. After the class declaration add permits clause to give the permission to those sub-classes which you want's to be inherited.