Java non-sealed Keyword

The non-sealed keyword in Java is used to declare that a class or interface that extends or implements a sealed class or interface is open to further subclassing. This feature, introduced in Java 17, allows for flexibility in extending a class hierarchy that would otherwise be restricted by the sealed keyword.

Table of Contents

  1. Introduction
  2. non-sealed Keyword Syntax
  3. Understanding non-sealed
  4. Examples
    • Sealed Class with Non-Sealed Subclass
    • Sealed Interface with Non-Sealed Implementation
  5. Real-World Use Case
  6. Conclusion

Introduction

The non-sealed keyword provides a way to create exceptions in a sealed class hierarchy, allowing certain subclasses to be extended further while maintaining restrictions on others. This enables more flexible design decisions where certain parts of the class hierarchy need to remain extensible.

non-sealed Keyword Syntax

The syntax for declaring a non-sealed class or interface is as follows:

public non-sealed class ClassName extends SealedClass { // class body } public non-sealed interface InterfaceName extends SealedInterface { // interface body } 

Example:

public sealed class Shape permits Circle, Rectangle, Square { // class body } public non-sealed class Rectangle extends Shape { // class body } 

Understanding non-sealed

Key Points:

  • Exception to Sealing: The non-sealed keyword allows a subclass to be open for further subclassing even when the superclass is sealed.
  • Combined with sealed: Can be used in combination with sealed and final to create flexible and controlled inheritance hierarchies.

Examples

Sealed Class with Non-Sealed Subclass

A sealed class with a non-sealed subclass.

Example

// File: Shape.java public sealed class Shape permits Circle, Rectangle, Square { // common properties and methods } // File: Circle.java public final class Circle extends Shape { // properties and methods specific to Circle } // File: Rectangle.java public non-sealed class Rectangle extends Shape { // properties and methods specific to Rectangle } // File: Square.java public final class Square extends Shape { // properties and methods specific to Square } // File: RoundedRectangle.java public class RoundedRectangle extends Rectangle { // properties and methods specific to RoundedRectangle } 

Sealed Interface with Non-Sealed Implementation

A sealed interface with a non-sealed implementation.

Example

// File: Operation.java public sealed interface Operation permits Addition, Subtraction { int apply(int a, int b); } // File: Addition.java public final class Addition implements Operation { @Override public int apply(int a, int b) { return a + b; } } // File: Subtraction.java public non-sealed class Subtraction implements Operation { @Override public int apply(int a, int b) { return a - b; } } // File: AdvancedSubtraction.java public class AdvancedSubtraction extends Subtraction { // additional methods and properties } 

Real-World Use Case

Evolving Frameworks

In real-world applications, frameworks may have a sealed core class hierarchy to maintain control over certain aspects, while allowing users to extend specific parts of the framework for customization.

Example

// File: FrameworkComponent.java public sealed class FrameworkComponent permits CoreComponent, ExtensionComponent { // common properties and methods } // File: CoreComponent.java public final class CoreComponent extends FrameworkComponent { // properties and methods specific to CoreComponent } // File: ExtensionComponent.java public non-sealed class ExtensionComponent extends FrameworkComponent { // properties and methods specific to ExtensionComponent } // File: CustomExtension.java public class CustomExtension extends ExtensionComponent { // user-defined properties and methods } 

Conclusion

The non-sealed keyword in Java provides a way to create flexible and extensible class hierarchies within the constraints of a sealed class or interface. By using non-sealed, you can selectively allow further subclassing where necessary, balancing control and flexibility in your application’s design. Understanding and using the non-sealed keyword effectively is crucial for developing modular and maintainable Java applications.

Leave a Comment

Scroll to Top