Pages

Topics

Tools

↑↓navigate↵openescclose
All guides

Java non-sealed Keyword: Extending Sealed Types

The Java non-sealed keyword reopens a sealed type hierarchy to arbitrary subclasses. Learn how it works, when to use it, and how it affects subtype extensibility.

~4 min readJava
#Java#sealed classes#non-sealed#inheritance
Diagram showing a sealed class hierarchy with one non-sealed branch open to subclasses

The non-sealed keyword allows a subtype in a sealed Java hierarchy to opt out of the sealed restriction. This article explains its syntax, how it interacts with the permits clause, and what it means for pattern matching and maintainability.

In Java 17, sealed classes and interfaces let you declare a fixed set of permitted subtypes. The non-sealed modifier reopens a sealed hierarchy to unknown subtypes. It appears in the same position as final or sealed in a type declaration, and it tells the compiler that this particular subtype is free to be extended by any class or interface, even those outside the original permitted list.

What the non-sealed Keyword Does

When you declare a class or interface as sealed, you normally list every direct subtype in a permits clause. If the subtypes are in the same source file, the compiler can infer the list. Each permitted subtype must be either final, sealed, or non-sealed; records are implicitly final. A final subtype cannot be extended further. A sealed subtype continues the restriction by defining its own permitted list. A non-sealed subtype removes the restriction entirely: any class or interface can extend or implement it, regardless of whether it appears in the original permits list.

This is useful when you want to enforce a closed hierarchy at the top level but still allow open extension from a specific branch. For example, a library can define a sealed Shape interface with only Circle and Polygon as permitted subtypes. If Polygon is declared non-sealed, application code can define new polygon types without modifying the library.

Declaring a non-sealed Class or Interface

The syntax is straightforward. You place non-sealed before the class or interface keyword, in the same position where sealed or final would appear.

java
public sealed interface Shape permits Circle, Polygon { double area(); } public final class Circle implements Shape { // ... } public non-sealed class Polygon implements Shape { // ... }

Here, Polygon is non-sealed. That means any class can extend Polygon, even if it was not listed in the permits clause of Shape. The compiler will not complain about a new subclass of Polygon that appears in a different package or a third-party module.

The same rule applies to interfaces. A non-sealed interface can be implemented by any class, and other interfaces can extend it freely.

How non-sealed Affects Permitted Subtypes

The permits clause on a sealed type lists the direct subtypes. Each of those subtypes must be either final, sealed, or non-sealed. If you mark a permitted subtype as non-sealed, it effectively breaks the chain of restriction. Subtypes of that non-sealed type are not subject to the original sealed hierarchy's constraints.

This means the compiler can no longer guarantee that all implementations of Shape are known at compile time. Pattern-matching exhaustiveness still works if you cover the non-sealed branch with a pattern for the non-sealed type itself. For example, this switch is exhaustive because the Polygon case matches every current and future subclass of Polygon:

java
String describe(Shape shape) { return switch (shape) { case Circle c -> "circle"; case Polygon p -> "polygon"; }; }

The guarantee changes when you switch on a non-sealed type and try to enumerate only its known subclasses. Because new subclasses can appear outside the sealed hierarchy, that kind of switch is not exhaustive, and the compiler will require a default branch or a pattern for the non-sealed type itself.

Comparing non-sealed, sealed, and final

ModifierCan be extended?Must list permitted subtypes?Compiler exhaustiveness
finalNoNoGuaranteed for the type
sealedYes, but only listed subtypesYesGuaranteed for the type and its permitted subtypes
non-sealedYes, by any class or interfaceNoNot guaranteed beyond this type

The choice among these three modifiers determines how much control you keep over the type hierarchy. final closes the branch completely. sealed keeps the branch closed but allows a known set of extensions. non-sealed opens the branch to arbitrary extension, trading compile-time exhaustiveness for flexibility.

Runtime and Maintainability Considerations

At runtime, non-sealed has no direct performance cost. The JVM does not treat non-sealed types differently from ordinary classes or interfaces. The effect is purely at compile time, in the type checker and exhaustiveness analysis.

The maintainability tradeoff is more significant. A non-sealed type makes the overall hierarchy less predictable. Code that relies on exhaustive pattern matching must be updated when new subtypes appear. This is acceptable when the non-sealed branch is meant to be an extension point, such as a plugin interface or a base class in an open framework.

When designing a sealed hierarchy, use non-sealed sparingly. If every permitted subtype is non-sealed, the sealed keyword provides little benefit. The typical pattern is to have one or two non-sealed branches that serve as escape hatches, while the rest remain final or sealed.

Common Mistakes and Edge Cases

One common mistake is applying non-sealed to a subtype that was not listed in the permits clause. The compiler will reject this because non-sealed is only valid on a direct permitted subtype of a sealed type. You cannot declare a random class as non-sealed outside the hierarchy.

Another edge case involves records. Records are implicitly final, so you cannot declare a record as non-sealed or sealed. If you need a non-sealed subtype, it must be a class or an interface, not a record.

Also note that non-sealed is a contextual keyword. The compiler treats it as a modifier only in the context of a sealed type declaration; it is not an ordinary modifier outside that context.

When you migrate an existing class hierarchy to sealed, you must make sure each permitted subtype is final, sealed, or non-sealed; records are implicitly final. If you want to preserve open extensibility for a particular branch, non-sealed is the correct choice. If a permitted class has no modifier, the compiler will error, forcing you to make an explicit decision.

Try matching browser tools

All tools

Small utilities that fit Java work — they run entirely in this browser tab, so nothing you paste leaves your device.

All Java guides

More in Java

All Java guides

The guides filed next to this one in Java, in library order.