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
🌐
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.
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
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
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
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.

🌐
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.
🌐
TechWell
techwell.com › techwell-insights › 2021 › 09 › sealed-classes-java-17
Sealed Classes in Java 17 | TechWell
September 15, 2021 - Sealed classes in Java 17 is a new feature and not a “preview” feature. A class is declared sealed using the modifier sealed. The classes that are allowed to extend a sealed class are declared with the modifier permits. The sealed and permits are not keywords but identifiers.
🌐
GeeksforGeeks
geeksforgeeks.org › java › sealed-class-in-java
Sealed Class in Java - GeeksforGeeks
2 weeks ago - A sealed class in Java is a class that restricts which other classes can extend it. The permitted subclasses are explicitly specified using the permits clause. This provides more control over inheritance than a normal class.
Find elsewhere
🌐
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 › codex › sealed-classes-in-java-17-8f00351d27f4
Sealed Classes in Java 17. An Introduction | by Afroz Chakure | CodeX | Medium
August 12, 2024 - Above, we have defined a sealed class, Shape, which allows only Circle, Square, and Triangle to extend it. public sealed interface Vehicle permits Car, Bike { void start(); }
🌐
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.
🌐
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 › @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.
🌐
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 ...
🌐
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.
🌐
Javaplanet
javaplanet.io › home › java 17 features › sealed classes
Sealed Classes -
September 6, 2025 - Sealed classes are a feature introduced in JEP 409 (part of Java 15 as a preview, finalized in Java 17) to provide more control over class inheritance.
🌐
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....
🌐
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.
🌐
C# Corner
c-sharpcorner.com › article › sealed-classes-a-java-17-new-feature
Sealed Classes - A Java 17 New Feature
September 5, 2022 - So here comes the concept called sealed classes where we can mention which are subclasses or sub interfaces which can inherit from this particular class and interface and we can do this with the help of sealed keyword.