4 minutes
Level Up Your Jakarta EE Apps with Payara 7 and New Docker Images
We’re excited to share major updates around the Docker image story for the Payara Platform Community, aligned with our […]
Sealed classes are a powerful feature introduced in Java 15 that allows developers to control the inheritance hierarchy of a class. In this blog, we will explore the concept of sealed classes in the context of Jakarta EE, a set of specifications for building enterprise applications in Java.
Sealed classes provide a mechanism to restrict the inheritance of a class. By specifying the sealed modifier before a class declaration, you define a finite set of classes that can inherit from it. This means that only classes explicitly listed as permitted subclasses can extend a sealed class. Any other attempt to extend the sealed class will result in a compilation error.
In the world ofJakarta EE, where building scalable and secure enterprise applications is paramount, sealed classes can offer several benefits:
To declare a sealed class, the ‘sealed’ modifier is used in the class declaration. For example:
public sealed class Vehicle permits Car, Truck, Motorcycle { ... }
In this example, the class ‘Vehicle’ is declared as sealed and allows only ‘Car’, ‘Truck’, and ‘Motorcycle’ to extend it.
When extending a sealed class, subclasses must be declared using the ‘non-sealed’ modifier. This indicates that the class is allowed to extend a sealed class. For example:
public non-sealed class Car extends Vehicle { ... }
Permitted subclasses are listed using the ‘permits’ keyword in the sealed class declaration. Only these classes are allowed to extend the sealed class. Other attempts to subclass will result in a compilation error. This is particularly useful when creating a family of classes that should be extended, but with a controlled and limited scope.
Custom Exception Hierarchies: Jakarta EE applications often deal with various exceptions to handle different error scenarios. Sealed classes can be used to create a custom exception hierarchy that allows only specific subclasses to extend a base exception class. This ensures that the exception hierarchy is well-defined and prevents unintended or uncontrolled exception classes from being created.
Authentication and Authorization Mechanisms: In a Jakarta EE application, you might have different authentication and authorization mechanisms to control access to certain resources or functionalities. Sealed classes can be used to define the types of authentication and authorization strategies available, allowing the application to support specific, well-defined strategies while preventing unauthorized extensions that could potentially compromise security.
Custom Annotations: Jakarta EE applications often use annotations to provide metadata and configuration. Sealed classes can be utilized to define a set of custom annotations, ensuring that only permitted annotation types can be used within specific contexts or frameworks. This helps maintain consistency and prevents misuse of annotations.
Extensible Business Rules: In some cases, a Jakarta EE application may require extensible business rules or policies that are subject to certain restrictions. Sealed classes can be employed to define the base business rule class and permit specific subclasses that adhere to predefined guidelines. This approach ensures that new business rules can be added in a controlled manner without compromising the application’s integrity.
API Design and Versioning: When developing APIs for Jakarta EE applications, using sealed classes can help maintain backward compatibility and provide a clear contract to the users of the API. By sealing classes and explicitly specifying permitted implementations, you can ensure that future versions of the API can introduce new implementations while maintaining compatibility with existing code.
Sealed classes offer a valuable tool in the Java language for controlling class inheritance. In the context of Jakarta EE, sealed classes can help enhance security, promote maintainability, and facilitate compatibility. By precisely defining the inheritance hierarchy, developers can build robust and scalable enterprise applications.
As you explore sealed classes in Jakarta EE, remember to adhere to the guidelines for declaring sealed classes, listing permitted subclasses, and handling exhaustive checks. By using sealed classes effectively, you can take advantage of their benefits and improve the quality of your Jakarta EE applications.
If you found this useful, try some of these other Jakarta EE guides:
Build Fast and Secure. ✅Supported.✅ Best for Jakarta EE and MicroProfile.✅
Share:
4 minutes
We’re excited to share major updates around the Docker image story for the Payara Platform Community, aligned with our […]
4 minutes
Keeping an application server running smoothly isn’t so much about new features, but more about predictability and consistency. Software […]
5 minutes
At Devoxx Belgium 2025, I was able to talk about what happens after you build your container. In theory, […]