Java permits clause: Sealing Class Hierarchies
Learn how the Java permits clause restricts subclass hierarchies in sealed classes and interfaces, including syntax, constraints, reflection, and maintenance.

In Java, the permits clause is part of sealed classes and interfaces, a feature finalized in Java 17. It lets you declare exactly which classes or interfaces may extend or implement a given type. This turns an open inheritance model into a closed one, giving the compiler enough information to reason about all possible subtypes.
The Role of the permits Clause in Sealed Types
A sealed class or interface restricts its subclasses or implementors to a fixed set. The permits clause lists those allowed types explicitly. Without sealing, any class that can access the type and is not itself final could extend it, making it impossible for the compiler to know all subtypes at compile time.
Consider a simple domain model for a payment system. You want to allow only CreditCard, BankTransfer, and PayPal as payment methods. Sealing the PaymentMethod interface guarantees that no other implementation can exist outside the permitted set.
public sealed interface PaymentMethod permits CreditCard, BankTransfer, PayPal {
double amount();
}Here, permits is the clause that names the three allowed implementations. Any attempt to create a fourth implementing class directly will fail at compile time unless that class is added to the permits list.
Syntax and Placement of permits in a Sealed Class Declaration
The permits clause appears after the class or interface name and any extends/implements clauses, before the class body. It must list every direct subclass or implementor that is allowed. The order of the names does not matter, but each name must satisfy the access and location requirements described later.
public sealed class Shape permits Circle, Rectangle, Triangle {
// common shape logic
}
public final class Circle extends Shape {
// circle-specific implementation
}
public non-sealed class Rectangle extends Shape {
// rectangle-specific implementation
}
public final class Triangle extends Shape {
// triangle-specific implementation
}Each permitted class or interface must itself be constrained: ordinary classes are final, sealed, or non-sealed; subinterfaces are sealed or non-sealed. An enum or record subclass is implicitly final and therefore also allowed. A final class ends the hierarchy for that branch. A sealed class continues the restriction, with its own permitted subclasses declared explicitly or inferred from the source file. A non-sealed class opens the hierarchy again, allowing unknown subclasses.
If all direct subclasses are declared in the same source file as the sealed type, the compiler can infer them, and the permits clause is optional. If any direct subclass is declared in a different source file, the permits clause is required.
Constraints on Permitted Subclasses: Location and Modifiers
The permits clause imposes strict rules on where permitted subclasses can live. In Java 17, each permitted subclass must be in the same module as the sealed class. If the sealed class is in the unnamed module, the subclasses must be in the same package. This constraint prevents the hierarchy from spanning unrelated parts of the codebase.
Another constraint is that a permitted subtype cannot be anonymous or local. It must be a named top-level or nested type. This keeps the hierarchy explicit and inspectable.
The modifiers on permitted types also matter. A final class cannot be extended further. A sealed type continues the restriction; its own direct subtypes must be final, sealed, or non-sealed and, when necessary, named in its own permits clause. A non-sealed type can be extended or implemented by anyone, which reintroduces open inheritance for that branch.
public sealed class Vehicle permits Car, Truck {
}
public sealed class Car extends Vehicle permits Sedan, SUV {
}
public final class Sedan extends Car {
}
public final class SUV extends Car {
}
public non-sealed class Truck extends Vehicle {
}In this example, Car is sealed and restricts its own subclasses. Truck is non-sealed, so any class can extend it. This gives you fine-grained control over how much of the hierarchy remains closed.
How the Compiler Enforces Exhaustiveness in Switch Expressions
One of the main benefits of the closed subtype set is that the compiler can verify that a switch expression over a sealed type is exhaustive. Because the sealed type declares all direct subtypes, the compiler knows every possible direct implementation and can require a case for each one.
public String describe(PaymentMethod method) {
return switch (method) {
case CreditCard c -> "Credit card payment of " + c.amount();
case BankTransfer b -> "Bank transfer of " + b.amount();
case PayPal p -> "PayPal payment of " + p.amount();
};
}If you omit one of the permitted types, the compiler reports an error because the switch expression is not exhaustive. This moves a whole class of missing-case mistakes to compile time. Without a sealed subtype set, the compiler would have no way to know whether the switch covers all possible subtypes.
The case CreditCard c -> syntax uses type patterns in switch. Pattern matching for switch was previewed in Java 17 and became standard in Java 21. The exact exhaustiveness rules also depend on whether you are writing a switch expression or a switch statement; switch expressions require exhaustiveness.
permits vs. no permits: When the Clause Is Optional
The permits clause is not always required. If all permitted subclasses are declared in the same source file as the sealed type, the compiler can infer them. In that case, you can omit the clause entirely.
public sealed class Expression {
}
final class Literal extends Expression {
}
final class BinaryOp extends Expression {
}
final class UnaryOp extends Expression {
}Here, Expression is sealed, and its three subclasses are in the same file. The compiler knows them without an explicit permits clause. This reduces verbosity when the hierarchy is small and self-contained.
However, if any subclass is in a different file, the permits clause becomes mandatory. The compiler must know exactly which types are allowed, and it cannot scan the entire package or module for subclasses. This is a deliberate design choice to keep the contract explicit and to avoid accidental subclassing.
When you use the permits clause, every listed subclass must be directly related. You cannot list a subclass that is not actually declared to extend the sealed type. The compiler checks that each permitted type is a direct subclass or implementor.
Maintaining a Sealed Hierarchy: Adding and Removing Permitted Types
The permits clause affects how you evolve a class hierarchy. Adding a new permitted type requires changing the sealed class declaration and the permits clause. Existing exhaustive switch expressions that do not handle the new type will need to be updated.
Removing a permitted type is more disruptive. If you delete a subclass from the permits clause, the subclass can no longer extend the sealed type; the subclass declaration must also be removed or changed, and any code that references that subclass will fail to compile. This makes the hierarchy explicit but also more rigid.
For library authors, this rigidity is often desirable. It prevents consumers from extending internal hierarchies in unintended ways. It also makes API evolution predictable because the set of subtypes is part of the public contract.
A common maintenance pattern is to use non-sealed for a branch that you expect to grow. For example, a Shape hierarchy might have a Polygon subclass that is non-sealed, allowing library users to create custom polygon types. The rest of the hierarchy remains sealed.
Compatibility and Reflection: What Changes at Runtime
Sealed classes do not introduce new runtime bytecode instructions. The restriction is enforced by the compiler and recorded as metadata in the class file, and the JVM also enforces it when an unpermitted subclass is loaded or resolved. At runtime, a sealed class behaves like a regular class for ordinary use.
This metadata is accessible through reflection. The Class class has a method getPermittedSubclasses() that returns an array of Class<?> objects representing the permitted subclasses. This allows frameworks and tools to inspect the hierarchy at runtime.
Class<?> shapeClass = Shape.class;
Class<?>[] permitted = shapeClass.getPermittedSubclasses();
for (Class<?> subclass : permitted) {
System.out.println(subclass.getName());
}If a class is not sealed, getPermittedSubclasses() returns null. This is a useful way to detect whether a type is sealed without relying on isSealed().
The isSealed() method reports whether the class or interface is sealed. Combined with getPermittedSubclasses(), you can build generic utilities that handle sealed hierarchies dynamically.
One important runtime consideration is serialization. If a sealed class implements Serializable, the permitted subclasses must also be serializable if they are to be serialized. The permits clause does not automatically make subclasses serializable. You still need to manage serialization explicitly.
Because the sealed contract is part of the class file, a class that attempts to extend a sealed type without being listed as a permitted subclass is rejected with IncompatibleClassChangeError.
Try matching browser tools
All toolsSmall utilities that fit Java work — they run entirely in this browser tab, so nothing you paste leaves your device.
Related Java guides
All Java guidesJava Final Keyword: Usage and Behavior
How Java's final keyword works for variables, methods, and classes, with practical examples and decision guidance for using it.
Java Final Variable: Syntax, Behavior, and Use Cases
Understand what a final variable in Java means, how initialization works, and when to use it for constants, immutability, and clearer code.
Java Duration vs Period: When to Use Each
java duration vs period: Compare Java Duration and Period to handle time-based and date-based calculations correctly. Learn syntax, conversion, and pitfalls.
Instance vs Static Variables in Java: Key Differences
Understand the difference between instance and static variables in Java, including memory, lifecycle, and when to use each.
More in Java
All Java guidesThe guides filed next to this one in Java, in library order.