> ## Documentation Index
> Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Java Sealed Interface

A sealed interface is an interface that restricts which classes or interfaces may directly implement or extend it. By using the `sealed` modifier in conjunction with the `permits` clause, a sealed interface explicitly defines an exhaustive list of allowed subtypes at compile time, providing strict, compiler-enforced control over the inheritance hierarchy.

## Syntax Declaration

To declare a sealed interface, apply the `sealed` modifier before the `interface` keyword. Following the interface name (and any generic type parameters), use the `permits` keyword followed by a comma-separated list of the fully qualified or simple names of the allowed subtypes.

```java theme={"dark"}
public sealed interface Shape permits Circle, Rectangle, Polygon {
    double calculateArea();
}
```

If the permitted subtypes are declared within the same source file as the sealed interface, the `permits` clause can be omitted. The Java compiler will automatically infer the permitted subtypes from the file's contents.

## Subtype Constraints and Rules

The Java compiler enforces strict rules on any class or interface listed in the `permits` clause of a sealed interface:

1. **Module and Package Restrictions:** Permitted subtypes must reside in the same module as the sealed interface. If the interface is declared in an unnamed module, the permitted subtypes must reside in the same package.
2. **Direct Implementation:** Every permitted subtype must directly implement or extend the sealed interface.
3. **Mandatory Modifiers:** Every permitted subtype must explicitly declare exactly one of three inheritance modifiers to define how the hierarchy continues:
   * `final`: Prevents any further extension. *(Note: This is only applicable to implementing classes, as interfaces cannot be declared `final`.)*
   * `sealed`: Continues the restriction, requiring the subtype to define its own `permits` clause.
   * `non-sealed`: Re-opens the hierarchy, allowing any unknown class or interface to extend or implement this specific subtype.

## Subtype Implementation Examples

The following code demonstrates how permitted subtypes must apply the mandatory modifiers when implementing or extending a sealed interface:

```java theme={"dark"}
// 1. A final class implementing the sealed interface
public final class Circle implements Shape {
    public double calculateArea() { 
        return Math.PI * 10 * 10; 
    }
}

// 2. A sealed interface extending the parent sealed interface
public sealed interface Rectangle extends Shape permits Square {
    double getWidth();
    double getHeight();
}

// 3. A non-sealed class implementing the sealed interface
public non-sealed class Polygon implements Shape {
    public double calculateArea() { 
        return 0.0; 
    }
}
```

## Integration with Record Classes

Java Records are implicitly `final`. When a record implements a sealed interface, it satisfies the mandatory modifier requirement without needing an explicit `final` declaration.

```java theme={"dark"}
public sealed interface Command permits PrintCommand {}

// The record is implicitly final, satisfying the sealed hierarchy rules
public record PrintCommand(String text) implements Command {}
```

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor Java Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
