Sealed Classes in Java:
Sealed Classes in Java are Introduced in Java 17 (JEP 409), sealed classes offer a powerful mechanism for developers to establish fine-grained control over inheritance hierarchies.This feature strengthens type safety, improves code maintainability, and enhances library design by restricting which classes can extend a sealed class.
Key Concepts and Syntax
sealed
Modifier: The cornerstone of sealed classes, this keyword declares a class that can only be extended by its designated permitted subclasses.
permits
Clause: Following the extends
and implements
clauses (if any), the permits
clause specifies the allowed subclasses. These subclasses must either be:
- Sealed: Can be further extended by their own permitted subclasses.
- Non-sealed: Can be extended by arbitrary classes in the future (though this is generally discouraged).
- Final: Cannot be extended at all.
Example: Shape Hierarchy with Sealed Classes In Java
sealed class Shape permits Circle, Rectangle {
public abstract double getArea();
}
final class Circle extends Shape {
private final double radius;
public Circle(double radius) {
this.radius = radius;
}
@Override
public double getArea() {
return Math.PI radius radius;
}
}
final class Rectangle extends Shape {
private final double width;
private final double height;
public Rectangle(double width, double height) {
this.width = width;
this.height = height;
}
@Override
public double getArea() {
return width height;
}
}
In this example:
Shape
is a sealed class, restricting its direct extensions.- The
permits
clause allows onlyCircle
andRectangle
to inherit. Circle
andRectangle
are both final, preventing further subclassing.
You can see second example of sealed class step by step in below video :
Benefits and Use Cases
- Enhanced Type Safety: Sealed classes prevent unexpected behavior by ensuring that only authorized subclasses can extend them. This reduces the risk of runtime errors and improves code predictability.
- Improved Maintainability: By explicitly defining the allowed hierarchy, sealed classes make it clear which extensions are valid and can be easily maintained. They also provide a safety net, preventing accidental modifications that might break existing code.
- Secure Library Design: Libraries can leverage sealed classes to control how their classes are extended. This safeguards the library’s integrity and prevents unintended modifications.
Additional Considerations and Best Practices
- Sealed Class Restrictions: All permitted subclasses of a sealed class must reside in the same module (typically the same JAR file or project). This ensures proper encapsulation and versioning control.
non-sealed
Subclasses: Usenon-sealed
subclasses cautiously, as they allow for uncontrolled future extensions. Consider making them sealed eventually to maintain control over the hierarchy.- Future-Proofing: By explicitly defining permitted subclasses, sealed classes offer some degree of future-proofing. New subclasses can be added in subsequent versions without breaking existing code that relies on the known hierarchy.
In conclusion, sealed classes represent a valuable addition to Java’s inheritance model. They promote controlled inheritance, improve type safety, enhance code maintainability, and facilitate the creation of more secure and predictable library designs. By understanding their concepts, syntax, and best practices, developers can effectively leverage sealed classes to create robust and well-structured Java applications.