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 - 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
🌐
LinkedIn
linkedin.com › all › javase
How do you use the records and sealed classes features introduced in Java 16 and 17?
March 11, 2024 - To enable records and sealed classes in Java 16 and 17: For Java 16, use the record keyword for records and sealed keyword for sealed classes with the permits clause specifying permitted subclasses.
Discussions

class - What are sealed classes in Java 17? - Stack Overflow
Today, I updated my Java version from 16 to 17, and I found that sealed classes is a new feature in it. I think it can be declared like this: public sealed class Main permits AClass, AnotherClass {... 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
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
Effectively Sealed Classes in Java

Sealed types become a lot less useful without the exhaustiveness checks.

More on reddit.com
🌐 r/java
18
24
November 16, 2019
🌐
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.
🌐
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.
🌐
Tutorialspoint
tutorialspoint.com › java › java_sealed_classes.htm
Java - Sealed Classes and Interfaces
Java 16 provides some minor enhancements and keeps this feature as a Preview. With Java 17, sealed class and interface a standard features. A sealed class/interface feature is added to Java to provide developers a fine fine-grained control over inheritance...
🌐
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.
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
🌐
Javaplanet
javaplanet.io › home › java 17 features › sealed classes
Sealed Classes -
September 6, 2025 - They allow a class to restrict which classes or interfaces can extend or implement it, enabling more robust and maintainable hierarchies. A class is declared with the sealed modifier. It must specify the permitted subclasses using the permits clause. ... public sealed class Shape permits Circle, ...
🌐
LinkedIn
linkedin.com › pulse › sealed-classes-java-17-afroz-chakure-xvxqf
Sealed Classes in Java 17
February 7, 2024 - 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 to restrict the inheritance hierarchy of a class or interface.
🌐
DZone
dzone.com › coding › java › effectively sealed classes in java
Effectively Sealed Classes in Java
August 16, 2018 - If we try to further extend Option, it’s no longer possible as long as we can’t add the extension to the file with the base class — and that’s the essence of class sealing. Furthermore, we could leverage additional compiler support, for example, when working with pattern matching. You can find more examples in Kotlin, as well. Since we don’t have a dedicated solution, we need to try to construct our own. In Java, Optional is implemented in a totally different way, because it’s designed to become a value type in the future — but that’s another story for another 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
Sealed classes were first introduced as a preview feature in JDK 15 (via [JEP 360](https://openjdk.org/jeps/360)) and refined in JDK 16 (JEP 397) before being finalized in **Java 17** (JEP 409) as a permanent language feature. As part of Java 17’s Long-Term Support (LTS) release, sealed 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 - 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 …
🌐
javathinking
javathinking.com › blog › sealed-classes-and-interfaces-in-java
Sealed Classes and Interfaces in Java: A Comprehensive Guide — javathinking.com
Java 16+ allows pattern matching with instanceof, eliminating the need for explicit casting. When combined with sealed types, using switch pattern matching can enforce exhaustive checking of all subtypes:
🌐
Medium
medium.com › @ucgorai › understanding-sealed-classes-in-java-when-and-how-to-use-sealed-final-and-non-sealed-5b1cf7d407ac
Understanding Sealed Classes in Java: When and How to Use sealed, final, and non-sealed | by Uma Charan Gorai | Medium
July 4, 2025 - Controlled Extensibility: Only specific classes can extend/implement the sealed class/interface. It improves code safety by controlling which classes are allowed to extend a base class. Better Readability: Easier to understand all possible sub classes. Exhaustive Pattern Matching: Enables better compiler checks for switch/case patterns, like in switch over instanceof or pattern matching. Stronger Encapsulation: Prevents unwanted sub classing, especially useful in public APIs.
🌐
Coderanch
coderanch.com › t › 751848 › java › Sealed-Classes
Sealed Classes (Java in General forum at Coderanch)
These are brand new to Java 17. From what I understand reading the book it is a class that includes a permit list that white lists classes that can directly extend it. The sealed keyword can also be applied to interfaces to white list classes that can implement or interfaces that can extend it.
🌐
DEV Community
dev.to › birajdar › java17-sealed-classes-4pl4
Java17 sealed classes - DEV Community
October 31, 2023 - Sealed classes are a feature introduced in Java 16 (as a preview feature) and finalized in Java 17. In this complete blog tutorial, we'll see what sealed classes are and why they're useful.
🌐
Medium
medium.com › @jeffersonfabriciodev › o-impacto-de-records-e-sealed-classes-no-java-16-663bc49b0834
O Impacto de records e sealed classes no Java 16+ | by Jefferson Fabrício | Medium
October 14, 2024 - Controle de hierarquia: Ajuda a controlar a extensão de uma classe, evitando heranças indesejadas ou imprevisíveis. Segurança em polimorfismo: Fornece uma estrutura clara para switch e pattern matching, já que todas as subclasses são conhecidas. ... public sealed class Forma permite Circulo, Retangulo {} public final class Circulo extends Forma {} public final class Retangulo extends Forma {}
🌐
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 - Sealed classes (explained in detail in JEP 360) provide a fine-grained mechanism that allows a developer to restrict which other classes or interfaces may extend them. You can think of final classes as the ultimate sealed class since no other ...