ADocumentation Index
Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
Use this file to discover all available pages before exploring further.
non-sealed class is a subclass within a sealed inheritance hierarchy that explicitly terminates the extension restrictions imposed by its sealed superclass. Introduced in Java 17, the non-sealed modifier reverts the class to Java’s default, open inheritance model, allowing any arbitrary, undeclared class to extend it.
When a standard class or interface inherits from a sealed parent, the Java compiler mandates that the subclass explicitly declare its ongoing inheritance posture using one of three modifiers. This explicit declaration rule does not apply to record or enum types, as records are implicitly final and enums are implicitly final or sealed. For standard classes, the modifiers are:
final: Completely prevents further extension.sealed: Continues the restricted hierarchy by requiring its ownpermitsclause.non-sealed: Opens that specific branch of the hierarchy to unrestricted extension.
Syntax and Implementation
Thenon-sealed modifier must be placed before the class or interface keyword. It is the first hyphenated keyword in the Java language.
The following example demonstrates a valid single-file compilation unit. The subclasses omit the public modifier so they can legally reside in the same file as the public root class.
Technical Characteristics
- Hierarchy Requirement: The
non-sealedmodifier is only valid on a class or interface that directly extends asealedclass or implements asealedinterface. Applying it to a standalone class results in a compilation error. - Pattern Matching Exhaustiveness: When using pattern matching for
switchexpressions on the rootsealedclass, the compiler considers thenon-sealedsubclass as a single exhaustive branch. The compiler does not require (or expect) you to handle the infinite potential subclasses of thenon-sealedclass. - Interface Application: Interfaces cannot be marked
final. Therefore, an interface extending asealedinterface must be marked eithersealedornon-sealed. - Reflection: At the bytecode level, a
non-sealedclass does not possess thePermittedSubclassesattribute. InvokingClass::isSealedon anon-sealedclass evaluates tofalse.
Master Java with Deep Grasping Methodology!Learn More





