Sealed Classes in Java 17
An Introduction
What are Sealed Classes?
Sealed classes are a feature introduced in Java 16 (as a preview feature) and finalized in Java 17.
Sealed classes allow developers to restrict the inheritance hierarchy of a class or interface. In other words, they provide more control over how a class can be extended or implemented.
Code Example for a Sealed class
Take a Shape
class, which can be extended by Circle
, Square
, or Triangle
classes. With Sealed Classes, we can specify that only these three classes can inherit from Shape
, and no other class can extend it.
Check syntax below:
public sealed class Shape permits Circle, Square, Triangle {
// Class definition
}
Above, we have defined a sealed class, Shape, which allows only Circle, Square, and Triangle to extend it.
1. Defining a sealed class
public sealed interface Vehicle permits Car, Bike {
void start();
}
In this example, we have defined a sealed interface calledVehicle
which permits only Car
and Bike
classes to implement it. The interface defines a single method called start
().
2. Extending a sealed class
public final class Circle extends Shape {
private final double radius;
public Circle(double radius) {
this.radius = radius;
}
// Class definition
}
In above example, we have defined a class Circle
which extends sealed class Shape.
The Circle class has a parameterized constructor that takes radius as parameter and initializes the radius field.
3. Attempting to Extend an Unpermitted Class
public class Rectangle extends Shape {
private final double width;
private final double height;
public Rectangle(double width, double height) {
this.width = width;
this.height = height;
}
// Class definition
}
In this example we have defined a class Rectangle
, which extends sealed class Shape.
However, since Rectangle
is not listed as one of the permitted classes, we get a compile-time error.
Features of Sealed Classes:
- It helps creating a finite set of classes in inheritance.
- Sealed classes specify which classes can extend them, providing a clear hierarchy.
- They ensure that only permitted sub-classes are created, avoiding unexpected behavior.
- Useful for creating frameworks, allows developers to dictate which classes users can extend.
- Especially beneficial in large projects, it prevents unintentional changes to important classes.
Benefits of Sealed Classes:
Sealed Classes provide several benefits, including:
- Enhanced Security: With Sealed Classes, developers can restrict the inheritance hierarchy of a class or interface, preventing unauthorized access to sensitive code.
- Better Maintainability: By restricting the inheritance hierarchy, Sealed Classes promote code maintainability by making it easier to understand and modify the code-base.
- Improved Performance: Sealed Classes enable the JVM to make more optimizations at runtime, leading to improved performance.
Important points on Sealed classes:
- Sealed class must specify it’s permitted sub-class using permits clause.
- Sealed class must have sub-classes.
- Only the listed sub-classes are allowed to extend the sealed class.
- Permitted sub-classes must be in same package.
- Permitted sub-classes should extends their sealed super class directly.
- Permitted sub-classes should extends their sealed super class directly.