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
🌐
Baeldung
baeldung.com › home › java › core java › sealed classes and interfaces in java
Sealed Classes and Interfaces in Java | Baeldung
December 11, 2025 - This feature enables more fine-grained inheritance control in Java. Sealing allows classes and interfaces to define their permitted subtypes. In other words, a class or interface can define which classes can implement or extend it.
Discussions

class - What are sealed classes in Java 17? - Stack Overflow
Writing with sealed classes makes ... things in larger code repositories with a large amount of developers working on it. 2021-09-27T07:05:14.253Z+00:00 ... One need workarounds creating mock objects for sealed classes. I see, the clear structure is a nice thing, but if it as useful, that we need a special keyword. We have already "final" with very similar purpose... 2021-09-27T07:56:58.897Z+00:00 ... @30thh Related - now that Kotlin is moving to Java 17 byte code ... 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
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 Sealed Classes
I'm looking forward to this! I love it when the type system / compiler can keep me from making mistakes. More on reddit.com
🌐 r/java
44
87
June 17, 2020
🌐
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.
🌐
JavaTechOnline
javatechonline.com › home › sealed class in java
Sealed Class In Java With Examples
May 3, 2026 - We use the ‘sealed’ keyword to declare a class as sealed class in java along with the permits clause to specify which classes are allowed to extend it.
🌐
Oracle
docs.oracle.com › en › java › javase › 17 › language › sealed-classes-and-interfaces.html
Java Language Updates
October 20, 2025 - However, as Polygon is non-sealed, it can be extended. However, no potential subtype of Polygon can extend UtahTeapot as UtahTeapot is final. Therefore, it's impossible for a Shape to be a UtahTeapot. In contrast, the second cast statement Ring r = (Ring) s is allowed; it's possible for a Shape to be a Ring because Ring is not a final class. ... java.lang.constant.ClassDesc[] permittedSubclasses(): Returns an array containing java.lang.constant.ClassDesc objects representing all the permitted subclasses of the class if it is sealed; returns an empty array if the class is not sealed
🌐
Medium
heshanu97.medium.com › demystifying-the-java-sealed-keyword-adfb64026764
Demystifying the Java Sealed Keyword. | by Heshan Umayanga | Medium
July 26, 2025 - We can use the extends keyword to show inheritance concepts in Java. There is no limit for subclasses for this process. ... Sealing allows classes and interfaces to define their permitted subtypes.
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.

Find elsewhere
🌐
OpenJDK
openjdk.org › jeps › 409
JEP 409: Sealed Classes
January 27, 2021 - A permitted subclass may be declared sealed to allow its part of the hierarchy to be extended further than envisaged by its sealed superclass, but in a restricted fashion. A permitted subclass may be declared non-sealed so that its part of the hierarchy reverts to being open for extension by ...
🌐
Medium
medium.com › @barbieri.santiago › java-17-sealed-interfaces-c18ab61a0322
Java 17: Sealed Interfaces. The concept of sealed types was… | by Santiago | Medium
December 18, 2024 - The sealed keyword restricts which classes or interfaces can extend/implement a class/interface.
🌐
Tutorialspoint
tutorialspoint.com › java › java_sealed_classes.htm
Java - Sealed Classes and Interfaces
Sealed classes allow to declaration of which class can be a subtype using the permits keyword. A class extending sealed class must be declared as either sealed, non-sealed, or final.
🌐
Aegissofttech
aegissofttech.com › home › insights › exploring java sealed classes and interfaces: a comprehensive guide
Exploring Java Sealed Classes and Interfaces
October 2, 2025 - Java sealed classes and interfaces, ... using the sealed keyword, you can explicitly define the set of permitted subclasses or implementers, ensuring tighter control over your class hierarchy....
🌐
Rollbar
rollbar.com › home › what are sealed classes in java?
Beginner’s Guide to Sealed Classes in Java | Rollbar
November 10, 2023 - They provide a mechanism for specifying which classes can extend a particular class, which helps prevent unauthorized extensions. The sealed modifier is used to declare a class as sealed. Additionally, the classes that are permitted to be its ...
🌐
HappyCoders.eu
happycoders.eu › java › sealed-classes
Sealed Classes in Java
June 12, 2025 - Since Java places a high value on backward compatibility, it was decided not to affect existing code as much as possible. That is made possible by so-called "contextual keywords" – keywords that only have a meaning in a specific context. The terms sealed and permits, for example, are such "contextual keywords" and have meaning only in the context of a class definition.
🌐
JDriven
jdriven.com › blog › 2021 › 10 › Sealed-classes
Sealed classes in Java 101 - JDriven Blog
October 19, 2021 - The non-sealed keyword opens a class for extension outside of the Root class (see class 'E'). Other classes will be able to extend these, but will count as 'E' when pattern matching over Root. A sealed class can also contain more sealed classes as long as they have at least 1 member (see class 'C' with member 'D'). It is also possible for Root to have member classes that are not in the same file.
🌐
YouTube
youtube.com › watch
Sealed Classes in Java | Java 17 features - YouTube
Check out our courses:Mastering Agentic AI with Java : https://go.telusko.com/agentic-aiCoupon: TELUSKO10 (10% Discount)DevOps Bootcamp: https://go.telusko...
Published: July 21, 2022
🌐
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 - ... A sealed class allows you to restrict or choose its sub-classes. A class can not extend a sealed class, if it is not in the list of permitted child classes of parent class. A class is sealed with the use of sealed keyword.
🌐
TheServerSide
theserverside.com › tip › Use-sealed-classes-in-Java-to-control-your-inheritance
Use sealed classes in Java to control your inheritance
4 weeks ago - A sealed class or interface explicitly controls which types may directly extend or implement it. Sealed classes became a permanent Java language feature in Java 17 through JEP 409.