Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

A 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:
  1. final: Completely prevents further extension.
  2. sealed: Continues the restricted hierarchy by requiring its own permits clause.
  3. non-sealed: Opens that specific branch of the hierarchy to unrestricted extension.

Syntax and Implementation

The non-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.
// 1. Root sealed class explicitly permitting three subclasses
public sealed class Shape permits Circle, Rectangle, Polygon {}

// 2. Terminal node: Cannot be extended
final class Circle extends Shape {}

// 3. Continued sealed node: Maintains restricted inheritance
sealed class Rectangle extends Shape permits Square {}
final class Square extends Rectangle {}

// 4. Non-sealed node: Terminates the restriction for this branch
non-sealed class Polygon extends Shape {}

// 5. Unrestricted extension: Any class can now extend Polygon
// without needing permission from Shape or Polygon
class Hexagon extends Polygon {}
class Octagon extends Polygon {}

Technical Characteristics

  • Hierarchy Requirement: The non-sealed modifier is only valid on a class or interface that directly extends a sealed class or implements a sealed interface. Applying it to a standalone class results in a compilation error.
  • Pattern Matching Exhaustiveness: When using pattern matching for switch expressions on the root sealed class, the compiler considers the non-sealed subclass as a single exhaustive branch. The compiler does not require (or expect) you to handle the infinite potential subclasses of the non-sealed class.
  • Interface Application: Interfaces cannot be marked final. Therefore, an interface extending a sealed interface must be marked either sealed or non-sealed.
  • Reflection: At the bytecode level, a non-sealed class does not possess the PermittedSubclasses attribute. Invoking Class::isSealed on a non-sealed class evaluates to false.
Master Java with Deep Grasping Methodology!Learn More