Explain the significance of the non-sealed keyword in Java.

Table of Contents

Introduction

The non-sealed keyword in Java was introduced with Java 17 as part of the enhancements to the object-oriented programming model. It works in conjunction with sealed classes, which were introduced to restrict which classes can extend or implement a given class or interface. The non-sealed keyword allows more flexibility in a controlled inheritance hierarchy by opening up a previously restricted class for extension by any class.

Understanding how non-sealed works within the context of sealed classes gives developers greater control over class hierarchies while balancing between tight control and flexibility.

Sealed Classes and Why non-sealed Matters

Sealed Classes in Java

Before diving into the non-sealed keyword, it's important to understand sealed classes. Sealed classes restrict which other classes or interfaces can extend them, helping developers design more predictable and controlled inheritance hierarchies. A sealed class explicitly declares which classes can extend it by using the permits clause.

Example of a sealed class:

In this example, only the Circle and Square classes are permitted to extend the Shape class.

Why non-sealed?

In some cases, you may want one subclass to continue restricting further extension (as sealed classes do), but another subclass to be open to any extensions. This is where the non-sealed keyword comes into play. By marking a subclass as non-sealed, you allow it to be extended freely, while other subclasses remain restricted.

The non-sealed keyword removes the restriction placed by sealed classes, creating more flexible inheritance hierarchies within a controlled structure.

How the non-sealed Keyword Works

The non-sealed keyword allows a class to be extended by any other class, even if it is part of a sealed hierarchy. This provides a balance where certain branches of a class hierarchy are restricted, while others remain open to extension.

Example of non-sealed Keyword Usage

In this example, Shape is sealed and only Circle, Square, and Triangle can extend it. However, Square is marked as non-sealed, meaning it can be extended by any other class, like ColoredSquare. Meanwhile, Circle remains sealed and cannot be extended further.

Practical Examples of non-sealed Keyword

Example 1: Allowing Specific Classes to Be Open for Extension

In a real-world scenario, you might have a class like Employee, where certain roles such as Manager should remain final, while others like Developer can be extended.

In this case, Manager is a final class, so it cannot be extended. However, Developer is marked as non-sealed, allowing other roles such as SeniorDeveloper to extend it freely.

Example 2: Mixing Sealed and Non-Sealed Hierarchies

In a banking system, you might want to seal high-level account types but allow variations in certain account types like savings or checking.

Here, BankAccount is sealed. CheckingAccount is non-sealed and can be extended by any other class, allowing further variations such as PremiumCheckingAccount. On the other hand, SavingsAccount is final, prohibiting any further extension.

Conclusion

The non-sealed keyword in Java adds flexibility to the strictness imposed by sealed classes, allowing you to open certain subclasses for further extension while keeping others restricted. It offers a controlled yet flexible approach to inheritance hierarchies, making it a useful tool for modern Java development. By strategically using non-sealed, developers can maintain a balance between strict design patterns and extendable systems.

With Java 17 and beyond, non-sealed enriches the ability to create more nuanced object-oriented designs, making it easier to control inheritance in a way that fits both security and flexibility requirements.

Similar Questions