0
0
JavaConceptBeginner · 4 min read

What is Sealed Class in Java: Explanation and Example

A sealed class in Java is a special class that restricts which other classes can extend or implement it. It helps control the class hierarchy by explicitly listing permitted subclasses using the permits keyword.
⚙️

How It Works

A sealed class works like a gatekeeper for inheritance. Imagine you have a family recipe book, but you only want certain trusted family members to add new recipes. Similarly, a sealed class lets you decide exactly which classes can extend it, blocking all others.

This is done by declaring the class as sealed and listing allowed subclasses with the permits keyword. The permitted subclasses must be in the same module or package and must themselves be declared as final, sealed, or non-sealed. This ensures a controlled and safe class hierarchy, improving code maintainability and security.

💻

Example

This example shows a sealed class Shape with three permitted subclasses: Circle, Rectangle, and Square. Each subclass provides its own implementation of the area() method.

java
public sealed abstract class Shape permits Circle, Rectangle, Square {
    public abstract double area();
}

public final class Circle extends Shape {
    private final double radius;
    public Circle(double radius) {
        this.radius = radius;
    }
    @Override
    public double area() {
        return Math.PI * radius * radius;
    }
}

public final class Rectangle extends Shape {
    private final double width, height;
    public Rectangle(double width, double height) {
        this.width = width;
        this.height = height;
    }
    @Override
    public double area() {
        return width * height;
    }
}

public final class Square extends Shape {
    private final double side;
    public Square(double side) {
        this.side = side;
    }
    @Override
    public double area() {
        return side * side;
    }
}

public class Main {
    public static void main(String[] args) {
        Shape c = new Circle(3);
        Shape r = new Rectangle(4, 5);
        Shape s = new Square(6);
        System.out.println("Circle area: " + c.area());
        System.out.println("Rectangle area: " + r.area());
        System.out.println("Square area: " + s.area());
    }
}
Output
Circle area: 28.274333882308138 Rectangle area: 20.0 Square area: 36.0
🎯

When to Use

Use sealed classes when you want to tightly control which classes can extend a base class. This is useful in scenarios like defining a fixed set of types or states, such as shapes, events, or commands, where you want to avoid unexpected subclasses.

Sealed classes improve code safety by preventing unauthorized extensions, making your design clearer and easier to maintain. They are especially helpful in large projects or APIs where you want to enforce strict inheritance rules.

Key Points

  • A sealed class restricts which classes can extend it using the permits keyword.
  • Permitted subclasses must be declared as final, sealed, or non-sealed.
  • Sealed classes improve code safety and maintainability by controlling inheritance.
  • This feature was introduced in Java 15 as a preview and became stable in Java 17.

Key Takeaways

Sealed classes limit which classes can extend them, improving control over inheritance.
Use the permits keyword to list allowed subclasses explicitly.
Permitted subclasses must be final, sealed, or non-sealed to complete the hierarchy.
Sealed classes help create safer and more maintainable code structures.
This feature is stable starting from Java 17.