0
0
JavaConceptBeginner · 3 min read

What is Sealed Interface in Java: Explanation and Example

A sealed interface in Java is an interface that restricts which other classes or interfaces can implement it. It allows you to control the set of permitted implementations, improving code safety and maintainability.
⚙️

How It Works

A sealed interface works like a gatekeeper. It lets you decide exactly which classes or interfaces are allowed to implement it. Think of it like a club with a guest list: only invited members can join.

This helps prevent unexpected or unwanted implementations, making your code easier to understand and safer to change. The permitted classes must be declared explicitly using the permits keyword in the sealed interface.

Also, the classes that implement a sealed interface must be either final, sealed, or non-sealed. This ensures the hierarchy is well controlled and clear.

💻

Example

This example shows a sealed interface Shape that only allows Circle and Rectangle to implement it.

java
public sealed interface Shape permits Circle, Rectangle {
    double area();
}

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

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

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

When to Use

Use sealed interfaces when you want to tightly control which classes can implement an interface. This is useful in situations where you want to enforce a fixed set of behaviors or types, such as defining a limited set of shapes, commands, or events.

It helps prevent accidental or unauthorized implementations, making your codebase more predictable and easier to maintain. For example, in a financial app, you might seal an interface representing transaction types to only allow specific known types.

Key Points

  • A sealed interface restricts which classes or interfaces can implement it.
  • Use the permits keyword to list allowed implementors.
  • Implementing classes must be final, sealed, or non-sealed.
  • Improves code safety by controlling the type hierarchy.
  • Introduced in Java 15 as a preview and standardized in Java 17.

Key Takeaways

A sealed interface controls which classes can implement it using the permits clause.
Implementing classes must declare their sealing status to maintain hierarchy control.
Sealed interfaces improve code safety and clarity by limiting implementations.
They are ideal for fixed sets of types or behaviors in your application.
Sealed interfaces became standard in Java 17.