Sub topic 4
Where Permitted Subclasses Can Live and When `permits` Can Be Omitted
This chapter assumes you can already declare a sealed type and pick final, sealed or non-sealed for each subclass. It covers two questions that come up as soon as a hierarchy spans more than one file. Where may the permitted subclasses be declared? And when can you leave out the permits clause?
The location rule
A sealed class or interface restricts its direct subtypes to a known set. The compiler also restricts where that set may be declared. The rule depends on how the code is compiled:
| Compilation setting | Permitted subclasses must be in |
|---|---|
| Named module (has a `module-info.java`) | The same module as the sealed type. Packages may differ. |
| Unnamed module (ordinary classpath code) | The same package as the sealed type. |
Both rules hold for every direct subtype, whether or not it is listed in permits. A class in a different module cannot extend your sealed type, even if it is public and you list it.
The restriction exists because the author of a sealed type needs to control the full set of subtypes. If a library could add subtypes from a different module, an exhaustive switch could not be trusted. Code in one module and code in another are normally built and shipped separately.
Case 1: everything in one compilation unit
A compilation unit is one source file. If the sealed type and all its direct subtypes are declared in the same file, you can omit permits. The compiler infers the permitted subclasses: every type in that file that directly extends or implements the sealed type.
This program is complete. Run it with java Main.java on Java 21 or later.
public class Main {
sealed interface Notification {}
record Email(String address) implements Notification {}
record Sms(String number) implements Notification {}
record Push(String deviceId) implements Notification {}
static String send(Notification n) {
return switch (n) {
case Email e -> "email to " + e.address();
case Sms s -> "sms to " + s.number();
case Push p -> "push to " + p.deviceId();
};
}
public static void main(String[] args) {
System.out.println(send(new Email("[email protected]")));
System.out.println(send(new Push("device-42")));
}
}All four types are nested in Main, so they share one compilation unit. The switch is exhaustive, and the compiler knows the inferred set is Email, Sms and Push.
Nesting is not required. The same inference works for several top-level types in one file:
sealed interface Expr {}
record Num(int value) implements Expr {}
record Neg(Expr inner) implements Expr {}Only one top-level type per file may be public, and it must match the file name. The other top-level types here are package-private. That is fine for a hierarchy used inside one package. For a public hierarchy, nest the subtypes inside the sealed type, as in the Expr example from the spine, so they can be public as well.
Inference only looks at the same file
Inference never looks at other files. Suppose Payment.java contains this:
public sealed interface Payment {}The compiler rejects this declaration, because a sealed type must have at least one permitted subclass. Adding Card.java with public record Card(String number) implements Payment {} does not help. Card is in another compilation unit, so it is not inferred. The compiler reports an error along the lines of "class is not allowed to extend sealed class: Payment (as it is not listed in its 'permits' clause)".
Case 2: separate files in one package
When the subtypes live in their own files, the permits clause is required. It names each subtype explicitly. This is the layout for classpath code:
// Payment.java
package com.shop;
public sealed interface Payment permits Card, Cash {}// Card.java
package com.shop;
public record Card(String number) implements Payment {}// Cash.java
package com.shop;
public record Cash() implements Payment {}Compile all three together, for example with javac -d out com/shop/*.java. Everything is in the package com.shop, which is the unnamed-module requirement.
Note that Card and Cash carry no explicit final. Records are implicitly final, so they satisfy the subclass-modifier rule. A plain class here would need final, sealed or non-sealed.
Case 3: different packages in a named module
Inside a named module, permitted subclasses may be in other packages of that module. A common reason is keeping implementations in an internal package that is not exported. The permits clause is required, because the subclasses are in other compilation units. It also needs imports (or qualified names) so the names resolve.
// module-info.java
module com.shop {
exports com.shop.payment;
}// com/shop/payment/Payment.java
package com.shop.payment;
import com.shop.payment.impl.Card;
import com.shop.payment.impl.Cash;
public sealed interface Payment permits Card, Cash {}// com/shop/payment/impl/Card.java
package com.shop.payment.impl;
import com.shop.payment.Payment;
public record Card(String number) implements Payment {}// com/shop/payment/impl/Cash.java
package com.shop.payment.impl;
import com.shop.payment.Payment;
public record Cash() implements Payment {}The impl package is not exported, so code in other modules sees Payment but cannot name Card or Cash. The hierarchy stays closed. If you compile the same files without module-info.java, you are in the unnamed module and the compiler rejects Card and Cash because they are in a different package from Payment.
Rules that apply to every `permits` clause
- Every listed class must directly extend (or, for an interface, directly implement) the sealed type. Listing a grandchild is an error.
- Every listed class must be accessible at the point of the
permitsclause. A package-private class in another package cannot be listed. - The order of the listed names does not matter to the compiler.
getPermittedSubclassesreports them in the order of the clause. - You may use qualified names such as
permits Expr.Num, Expr.Add. - A class cannot be permitted by a sealed type that it does not name in its own
extendsorimplements. Both directions must agree. - Anonymous classes and local classes cannot be permitted subclasses.
- A lambda cannot implement a sealed interface, even if it has a single abstract method, because a lambda creates a class that is not in the permitted set.
A permits clause may also be present when everything is in one file. It is then optional but legal, and it makes the closed set visible in the declaration. Once present, it is authoritative. A subclass in the same file that is not listed is an error.
Comparing the options
| Layout | `permits` | Why |
|---|---|---|
| Nested or same-file types | Optional (inferred) | The compiler can see every subtype. |
| Separate files, one package | Required | Inference does not cross files. |
| Separate packages, one named module | Required | Same module is enough, but the clause names the types. |
| Types in another module | Not possible | The location rule forbids it. |
For small hierarchies, omit permits and keep the types together. The set is easy to read in one place. For hierarchies with large or independently maintained implementations, use the explicit clause. A new file cannot join the hierarchy without a change to the sealed type, and that change shows up in code review.
Exercise
A project is compiled on the classpath with no module-info.java. Shape.java in package geo declares public sealed interface Shape permits geo.round.Circle {}, and Circle is a public record in package geo.round that implements Shape. Does it compile? What are two ways to fix it?
Show answer
It does not compile. In the unnamed module, permitted subclasses must be in the same package as the sealed type, and geo.round differs from geo. One fix is to move Circle into package geo. The other is to add a module-info.java so the code is compiled as a named module, since the same-module rule then allows Circle in another package of that module. Declaring Circle inside Shape.java also works, because it then shares the package and the compilation unit.
Exercise
You have sealed interface Light {} in Light.java and record Red() implements Light {} in Red.java, both in the same package. What error do you get, and what is the smallest change that fixes it?
Show answer
The compiler says Red is not allowed to extend the sealed interface because it is not in its permits clause. Inference only covers types in the same compilation unit, and Red is in another file. The smallest fix is to change Light to sealed interface Light permits Red {}. You could instead move Red into Light.java, which makes permits omittable.