Blog |

What are Sealed Classes in Java?

What are Sealed Classes in Java?
Table of Contents

Sealed classes are a feature introduced in Java 15 which bring a new level of control and predictability to class hierarchies. A sealed class is a class that explicitly specifies which other classes are allowed to extend it. This offers a way to restrict class hierarchies, making them more predictable and less prone to unintended extensions.

Why Sealed Classes?

In Java, class hierarchies can become complex, with numerous subclasses extending a common superclass. This can make it challenging to maintain and control the hierarchy. Unintentional extensions and modifications of classes can lead to unexpected issues and bugs in the codebase.

Sealed classes were introduced to address this challenge. They provide a mechanism for specifying which classes can extend a particular class, which helps prevent unauthorized extensions.

How to Use Sealed Classes

The sealed modifier is used to declare a class as sealed. Additionally, the classes that are permitted to be its direct subclasses are specified using the permits keyword. Here’s an example:

public sealed class Animal permits Dog, Cat, Bird {
    //Class implementation
}

In this example, the Animal class is declared as sealed and permits three subclasses: Dog, Cat and Bird. Any attempt to create a new subclass of Animal outside of this list will result in a compilation error.

Preventing Errors with Sealed Classes

Sealed classes offer several benefits in preventing errors and ensuring code correctness:

  • Controlled Extensibility: By explicitly specifying which classes can extend a sealed class, fine-grained control is achieved over the class hierarchy. This ensures that the hierarchy remains within the defined bounds, reducing the risk of unintended extensions and preserving code integrity.
  • Improved Readability: Sealed classes enhance code readability by clearly indicating which subclasses are allowed. Developers working with sealed classes immediately understand the intended structure of the hierarchy.
  • Early Error Detection: One of the major benefits of sealed classes is that they catch errors at compile time. If a developer attempts to create a new subclass that is not explicitly permitted, the compiler will issue an error.
  • Refactoring Safety: Sealed classes simplify the process of modifying class hierarchies. The permits list can be updated to reflect the new allowed subclasses, which makes refactoring safer and less error-prone.
  • API Stability: For library developers, sealed classes provide a means to maintain API stability. By sealing certain classes or interfaces, the parts of the API allowed to be extended by client code can be controlled. This prevents breaking changes in future releases, as client code won't be able to create unauthorized subclasses.

Recap

To tightly 'seal' our discussion: sealed classes in Java offer a mechanism for controlling class hierarchies, which helps prevent errors and improves code quality. By explicitly specifying which classes can extend a sealed class, you can write more predictable and maintainable code. Use sealed classes when building Java applications to provide early error detection, improved code readability and increased confidence when refactoring.

Track, Analyze and Manage Errors With Rollbar

Managing errors and exceptions in your code is challenging. It can make deploying production code an unnerving experience. Being able to track, analyze, and manage errors in real-time can help you to proceed with more confidence. Rollbar automates error monitoring and triaging, making fixing Java errors easier than ever. Try it today!

Related Resources

"Rollbar allows us to go from alerting to impact analysis and resolution in a matter of minutes. Without it we would be flying blind."

Error Monitoring

Start continuously improving your code today.

Get Started Shape