🌐
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.
🌐
Oracle
docs.oracle.com › en › java › javase › 17 › language › sealed-classes-and-interfaces.html
Sealed Classes
October 20, 2025 - Previous Next JavaScript must be enabled to correctly display this content · 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
Is there a way to make a regular Java class work like a Kotlin sealed class when being used from Kotlin?
What makes it not ergonomic to write the classes in Kotlin and use them in Java? Not saying you’re wrong just haven’t heard of that. More on reddit.com
🌐 r/Kotlin
9
3
November 30, 2023
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
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
🌐
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.
🌐
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.
🌐
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.
🌐
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.
Find elsewhere
🌐
4Comprehension
4comprehension.com › home › effectively sealed classes in java
Effectively Sealed Classes in Java
June 14, 2024 - If we try to extend Option further, it’s possible to add the extension to the file containing the base class – and that’s the essence of class sealing – which means that consumers of such API can’t create their extensions. Furthermore, we could leverage additional compiler support, such as extra exhaustiveness checks, when working with Pattern Matching. You can find it, for example, 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 differently because it’s a potential candidate to become a value type in the future – but that’s another story for another time.
🌐
Reddit
reddit.com › r/kotlin › is there a way to make a regular java class work like a kotlin sealed class when being used from kotlin?
r/Kotlin on Reddit: Is there a way to make a regular Java class work like a Kotlin sealed class when being used from Kotlin?
November 30, 2023 -

I have some classes/interfaces in Java that model variant types using a visitor pattern. It's a pretty flat type hierarchy, some interfaces that are unrelated to each other, each implemented by several final classes, something like:

public interface AOrB {}
public interface AOrBOrC {}
public final class A implements AOrB, AOrBOrC {}
public final class B implements AOrB, AOrBOrC {}
public final class C implements AOrBOrC {}

I'd like to make it so that Kotlin code calling this Java code treats the interfaces as sealed, so that I can use when expressions with exhaustiveness checks to match on these values instead of relying on the visitor pattern.

I'm aware that Java 15 has sealed classes, and based on some toy programs that seems to have the desired behavior. However, the Java part of the codebase is still on Java 8... so I was hoping that there's a way to do this without using Java's sealed classes.

Maybe some sort of annotation that adds additional typing information like @Nullable/@NonNull, which is somehow communicated to the Kotlin compiler? Not sure if something like that exists already, or if it's even possible.

I thought about trying to model it with inner classes, but I don't think it would work given that a class can implement multiple interfaces as shown in the snippet above.

Rewriting the classes in Kotlin doesn't seem to be an option either, since AIUI using a Kotlin sealed class from Java 8 is not ergonomic. But someone please correct me if I'm wrong.

🌐
Medium
medium.com › @sanjanarjn › sealed-classes-in-java-7444f7c706f5
Sealed classes in Java. Sealed classes got introduced in Java… | by Sanjana Rajan | Medium
February 1, 2024 - The basic idea of sealed classes or interfaces is restricting other classes or interfaces from extending or implementing them. Inheritance is a very powerful concept in Java and has proven extremely useful to model real world scenarios.
🌐
Netjstech
netjstech.com › 2022 › 11 › java-sealed-classes-and-interfaces.html
Java Sealed Classes and Interfaces | Tech Tutorials
Sealed class or interface lacks the permits clause and no class or interface from the same compilation unit declares Test as its direct superclass or superinterface · All the permitted subclasses must be accessible by the sealed class at compile time. For example, to compile Account.java, the compiler must be able to access all of the permitted classes of Account: SavingAccount.java, CurrentAccount.java and SalaryAccount.java.
🌐
Tutorialspoint
tutorialspoint.com › java › java_sealed_classes.htm
Java - Sealed Classes and Interfaces
Permitted subclass has to extend the sealed class. A permitted subclass has to use any one of final, sealed, or non-sealed modifier. In this example, we've created a sealed abstract class Person which permits Employee and Manager classes to extend it. Employee and Manager classes have different methods to get the ID of a person.
🌐
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 - A sealed class or interface restricts which other classes can extend or implement it. You list them with permits. Subclasses must themselves be final, sealed, or non-sealed. Sealed hierarchies are the foundation for exhaustive switches (Java 21+) — the compiler can prove you've handled every case.
🌐
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 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 ...
🌐
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.