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, you're not interested in allowing any arbitrary class to extend Shape; you don't want clients of your library declaring any further primitives. By sealing a class, you can specify which classes are permitted to extend it and prevent any other arbitrary class from doing so.
🌐
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
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
Making Use of Sealed Classes in Java

He’s a hero. 🤩

More on reddit.com
🌐 r/java
1
26
September 29, 2021
New candidate JEP: 360: Sealed Classes (Preview)
So many people complain about "But I want to override or access this private method" and this (and the module system) is ruining everything for me. Well, maybe you shouldn't have chosen a language/platform that has encapsulation and sandboxing as its cornerstones. I get that it can be super annoying, and I have been cursing some libraries (and the JDK) occasionally. But the alternative "One big mess of everything can access and override everything" is much worse IMO. More on reddit.com
🌐 r/java
68
44
May 8, 2020
🌐
Reddit
reddit.com › r/learnprogramming › why have sealed types in java?
r/learnprogramming on Reddit: Why have sealed types in Java?
June 27, 2024 -

I know a sealed type restricts the number of classes or interface that can directly extend or implement them. But why would you want to do that? What is the benefit of having control over the inheritance hierarchy?

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.

🌐
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.
🌐
Medium
medium.com › @toimrank › java-sealed-classes-ef82d1a5ab2f
Java Sealed Classes. Sealed classes is a powerful feature… | by Imran Khan | Medium
January 17, 2025 - Sealed classes are designed to restrict which classes or interfaces can extend or implement them. This feature allows developers to have more control over the class hierarchy, ensuring that only specific classes can be subtypes.
Find elsewhere
🌐
OpenJDK
openjdk.org › jeps › 409
JEP 409: Sealed Classes
January 27, 2021 - At the same time, the superclass should not unduly constrain its subclasses by, e.g., forcing them to be final or preventing them from defining their own state. A sealed class or interface can be extended or implemented only by those classes and interfaces permitted to do so.
🌐
Cinqict
cinqict.nl › blog › sealed-classes-and-functional-programming-in-java
Sealed classes and functional programming in Java
May 26, 2025 - By using a sealed interface List that only permits the records Empty and Cons, we can create an immutable algebraic data type (ADT) in Java. While some of Haskell’s compactness and conciseness is lost in translation, the result is still quite readable and maintains the functional programming style. The global functions, toString and addElement, are defined in a utility class, although they could also be implemented directly within the List interface.
🌐
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 - Java 17: Sealed Interfaces The concept of sealed types was introduced to restrict which classes or interfaces can extend or implement a given interface or class. This is for maintaining control over …
🌐
Kotlin
kotlinlang.org › docs › sealed-classes.html
Sealed classes and interfaces | Kotlin Documentation
June 29, 2026 - For an example, check out Use sealed classes with when expressions. Working with closed APIs: You want robust and maintainable public APIs for libraries that ensure that third-party clients use the APIs as intended. For more detailed practical applications, see Use case scenarios. Java 15 introduced a similar concept, where sealed classes use the sealed keyword along with the permits clause to define restricted hierarchies.
🌐
DEV Community
dev.to › adrian_nowosielski_7bd282 › sealed-classes-in-java-21-controlling-class-hierarchies-1moo
Sealed Classes in Java 21 – Controlling Class Hierarchies - DEV Community
October 8, 2025 - Sealed classes allow developers to restrict which other classes or interfaces may extend or implement them, giving more control over class hierarchies. This is particularly useful for APIs, libraries, or applications where you want to ensure ...
🌐
Medium
medium.com › @techiecuriosity › sealed-classes-in-java-21-enhancing-type-safety-and-maintainability-c1163a4256b4
Sealed Classes in Java: Enhancing Type Safety and Maintainability | by Tech Proverb | Medium
April 12, 2024 - Sealed Classes in Java: Enhancing Type Safety and Maintainability Sealed classes, introduced in Java 15 as a preview feature and made permanent in Java 17, are a powerful tool for controlling the …
🌐
Rollbar
rollbar.com › home › what are sealed classes in java?
Beginner’s Guide to Sealed Classes in Java | Rollbar
November 10, 2023 - Sealed classes are a feature introduced in Java 15 which bring a new level of control and predictability to class hierarchies. A sealed class is a class that explicitly specifies which other classes are allowed to extend it.
🌐
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 - Java sealed classes were introduced in Java 15 as a way to restrict the inheritance hierarchy of a class or interface.
🌐
TheServerSide
theserverside.com › tip › Use-sealed-classes-in-Java-to-control-your-inheritance
Java sealed classes: permits, non-sealed and pattern matching
1 month ago - A sealed hierarchy expresses the intended model directly in the Java type system. public sealed interface Transaction permits StockTransaction, BondTransaction, CryptoTransaction { } Each permitted implementation must then explicitly state what ...
🌐
Community Platform
wearecommunity.io › communities › java_americas › articles › 6984
The Power of Sealed Classes in Java
This article explores the concept of sealed classes in Java—a powerful language feature introduced to enhance type safety, control inheritance, and improve code maintainability. With clear explanations and practical examples, it’s a must-read for developers looking to master modern Java ...