🌐
DZone
dzone.com › coding › java › effectively sealed classes in java
Effectively Sealed Classes in Java
August 16, 2018 - One of the most popular sealed hierarchies that exist is Option. The classic implementation involves making Option a sealed abstract class and providing two subclasses: Some and None .
🌐
4Comprehension
4comprehension.com › home › effectively sealed classes in java
Effectively Sealed Classes in Java
June 14, 2024 - One of the most popular sealed hierarchies that exist out there is Option. The classic implementation involves making Option a sealed abstract class and providing two subclasses: Some and None.
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
C# sealed vs Java final - Stack Overflow
The equivalent to final for members in C# is readonly. ... That's because final in Java means plenty of different things depending on where you use it whereas sealed in C# applies only to classes and inherited virtual members (methods, properties, events). 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
Rust like Enums in Java
Just to make it a bit more clear, rust’s enums are a misnomer, they are sum types. More on reddit.com
🌐 r/java
86
93
May 2, 2023
🌐
Baeldung
baeldung.com › home › java › core java › sealed classes and interfaces in java
Sealed Classes and Interfaces in Java | Baeldung
December 11, 2025 - When creating the Vehicle abstract class in Java, we should be able to allow only Car and Truck classes to extend it. As such, we want to ensure that there will be no misuse of the Vehicle abstract class within our domain.
🌐
GeeksforGeeks
geeksforgeeks.org › java › sealed-class-in-java
Sealed Class in Java - GeeksforGeeks
2 weeks ago - All three classes directly extend Human, so each must be final, sealed, or non-sealed. Human references can point to objects of any of its permitted subclasses, demonstrating normal polymorphism. ... Control inheritance by allowing only specific subclasses. Define a predictable class hierarchy. Prevent unwanted extensions of a class. Improve maintainability by keeping the set of direct subclasses controlled. Work effectively with modern Java features such as pattern matching and exhaustive type handling.
🌐
Tutorialspoint
tutorialspoint.com › java › java_sealed_classes.htm
Java - Sealed Classes and Interfaces
Similar to sealed interface, a class can be marked as sealed class as well using sealed keyword and then using permits keywords, we can add the subclasses which can extend this class.
🌐
Oracle
docs.oracle.com › en › java › javase › 15 › language › sealed-classes-and-interfaces.html
3 Sealed Classes - Java
In addition, because Rectangle is a sealed class, the compiler also needs access to FilledRectangle.java.
🌐
C# Corner
c-sharpcorner.com › article › sealed-classes-in-java
Sealed Classes In Java
February 28, 2022 - In Java, a class can be sealed using the ‘sealed’ modifier and using the ‘permit’ clause we can specify the classes that are allowed to extend the Sealed class.
Find elsewhere
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.

🌐
OpenJDK
openjdk.org › jeps › 409
JEP 409: Sealed Classes
January 27, 2021 - When the permitted subclasses are small in size and number, it may be convenient to declare them in the same source file as the sealed class. When they are declared in this way, the sealed class may omit the permits clause and the Java compiler will infer the permitted subclasses from the declarations in the source file.
🌐
NashTech Blog
blog.nashtechglobal.com › home › sealed class: java vs kotlin
Sealed class: Java VS Kotlin - NashTech Blog Sealed class: Java VS Kotlin Application Engineering Java % % % %
June 22, 2026 - But if we want a way between these, Sealed class is the right choice. Sealed class provide inheritance for limited classes only. The concept of sealed class is officially introduced in Java 17 LTS version. But for Kotlin, it was added long before.
🌐
TheServerSide
theserverside.com › tip › Use-sealed-classes-in-Java-to-control-your-inheritance
Use sealed classes in Java to control your inheritance
1 month ago - That flexibility is useful, but sometimes a domain model should support only a known set of subtypes. Java sealed classes provide the middle ground between unrestricted inheritance and a completely closed final class.
🌐
Java With Us
javawithus.com › home › en › sealed classes and interfaces in java (java 17+)
Sealed Classes and Interfaces in Java (Java 17+) | Java With Us
April 21, 2026 - Sealed classes and sealed interfaces in Java 17+: closed hierarchies, permits clause, non-sealed subclasses, and exhaustive pattern matching in switch.
🌐
InfoQ
infoq.com › articles › java-sealed-classes
Java Feature Spotlight: Sealed Classes - InfoQ
June 17, 2020 - The release of Java SE 15 in Sept 2020 will introduce "sealed classes" (JEP 360) as a preview feature. A sealed class is a class or interface which restricts which other classes or interfaces may extend it.
🌐
JDriven
jdriven.com › blog › 2021 › 10 › Sealed-classes
Sealed classes in Java 101 - JDriven Blog
October 19, 2021 - If you are unfamiliar with sealed classes the easiest way to describe them is as an enum that can have multiple instances of each value. It is basically an inheritance trick where you can guarantee that your superclass is only implemented by a few specific subclasses.
🌐
JavaTechOnline
javatechonline.com › home › sealed class in java
Sealed Class In Java With Examples
May 3, 2026 - ClassName: The name of the sealed class or interface. permits Subclass1, Subclass2, …: Specifies the subclasses (for classes) or implementers (for interfaces) that are allowed. To declare a sealed interface in Java, we use the sealed keyword along with the permits clause to specify which classes or interfaces are allowed to implement/extend it.