Java Sealed class


In Java 17, sealed classes were introduced as a standard feature. Sealed classes provide a mechanism for restricting the inheritance hierarchy of a class, allowing the class author to control which classes can be subclasses. This enhances encapsulation and promotes more maintainable and robust code.

  • Sealed classes can improve code maintainability by making the class hierarchy more self-contained. It becomes easier to reason about the possible subclasses, and future changes or enhancements can be made with a clear understanding of the permitted subclasses.
  • Sealed classes also provide benefits for pattern matching. They can be used in instanceof checks and switch expressions to exhaustively handle all possible subclasses. This helps prevent unintentional gaps or redundancies in code that deals with class hierarchies.
  • Sealed classes contribute to better code maintainability, as they make it easier to understand and reason about the hierarchy of related classes. They provide stronger encapsulation by explicitly declaring which classes can be subclasses, reducing the risk of unauthorized subclasses interfering with the intended behavior of the sealed class.

Here’s an example that demonstrates the usage of sealed classes:

public sealed class Shape permits Circle, Rectangle {
    // Common properties and methods of Shape class

    public abstract double calculateArea();
}

final class Circle extends Shape {
    private final double radius;

    public Circle(double radius) {
        this.radius = radius;
    }

    public double calculateArea() {
        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;
    }

    public double calculateArea() {
        return width * height;
    }
}

In this example, we have a Shape class hierarchy consisting of a sealed class Shape and two subclasses Circle and Rectangle. The Shape class is declared as sealed using the sealed keyword, and the permits keyword specifies the permitted subclasses that can extend the sealed class.

The Shape class defines common properties and methods that are applicable to all shapes. It also declares an abstract method calculateArea() that is implemented by the subclasses.

The Circle and Rectangle classes extend the Shape class and provide their own implementations of the calculateArea() method specific to each shape.

By using sealed classes, we explicitly specify the permitted subclasses that can extend the Shape class. This provides better control over the class hierarchy and prevents unintended subclassing. For example, if a new shape class like Triangle is attempted to extend Shape, it would result in a compilation error since Triangle is not permitted by the Shape class.

With sealed classes, you have control over who can extend the sealed class. Only the explicitly permitted subclasses can extend the sealed class. If any other class attempts to extend the sealed class, a compilation error will occur.

It’s worth noting that sealed classes are part of the ongoing effort to enhance Java’s pattern matching capabilities, which aim to make code more expressive and less error-prone.