Sealed Classes
Sealed classes is a powerful feature got introduced as part of Java SE 15 as a preview and stable version in Java 17. Its a new way to control inheritance.
Sealed classes are designed to restrict which classes or interfaces can extend or implement them. This feature allows developers to have more control over the class hierarchy, ensuring that only specific classes can be subtypes. By doing so, sealed classes enhance the maintainability, security, and comprehensibility of the code.
A sealed class or interface specifies a limited set of permitted subclasses or implementations. These permitted subclasses must be explicitly declared within the sealed class or interface.
The subclasses can be one of the following:
Final: The subclass cannot be extended further.
Sealed: The subclass can be extended but only by a specified set of classes.
Non-Sealed / Non-Final: The subclass can be extended without restrictions.
Here’s the basic syntax:
public sealed class BaseClass permits SubClass1, SubClass2 {
// Class content
}
public final class SubClass1 extends BaseClass {
// Class content
}
public sealed class SubClass2 extends BaseClass permits SubSubClass {
// Class content
}
public final class SubSubClass extends SubClass2 {
// Class content
}
In above example, BaseClass is a sealed class that permits SubClass1 and SubClass2 to extend it. SubClass1 is a final class, while SubClass2 is a sealed class that permits SubSubClass to extend it.
Benefits of Sealed Classes
- Enhanced Control: Sealed classes provide fine-grained control over the class hierarchy, ensuring that only specific classes can extend or implement them.
- Improved Design: By restricting subclassing, sealed classes help enforce design constraints and maintain a clear and predictable class hierarchy.
- Better Security: Sealed classes prevent unauthorized or unintended extensions, reducing the risk of security vulnerabilities.
Simplified Maintenance: With a controlled set of subclasses, maintaining and evolving the class hierarchy becomes easier and less error-prone. - Exhaustive Pattern Matching: Sealed classes work well with pattern matching, allowing the compiler to perform exhaustive checks and ensure all possible cases are handled.
Sample Code
Let’s look at an example of using sealed classes to model a hierarchy of shapes:
// Sealed class Shape
public sealed class Shape permits Circle, Rectangle, Square {
// Common properties and methods for shapes
}
// Final subclass Circle
public final class Circle extends Shape {
private final double radius;
public Circle(double radius) {
this.radius = radius;
}
public double getRadius() {
return radius;
}
}
// Sealed subclass Rectangle
public sealed class Rectangle extends Shape permits Square {
private final double length;
private final double width;
public Rectangle(double length, double width) {
this.length = length;
this.width = width;
}
public double getLength() {
return length;
}
public double getWidth() {
return width;
}
}
// Final subclass Square
public final class Square extends Rectangle {
public Square(double side) {
super(side, side);
}
}
In this example, Shape is a sealed class that permits Circle, Rectangle, and Square to extend it. Circle and Square are final classes, while Rectangle is a sealed class that permits Square to extend it.

Imran Khan, Adobe Community Advisor, certified AEM developer and Java Geek, is an experienced AEM developer with over 12 years of expertise in designing and implementing robust web applications. He leverages Adobe Experience Manager, Analytics, and Target to create dynamic digital experiences. Imran possesses extensive expertise in J2EE, Sightly, Struts 2.0, Spring, Hibernate, JPA, React, HTML, jQuery, and JavaScript.