Sub topic 4
Where Permitted Subclasses Can Live and When `permits` Can Be Omitted
Assume you know Java inheritance, interfaces, packages, modules, records, and the final keyword. This chapter focuses on the location rules for permitted subclasses. These rules determine whether a sealed hierarchy can span several files or packages.
The location rule
A permitted subclass must be in the same module as its sealed superclass or interface.
For code that is not part of a named module, Java uses the unnamed module. In that case, the sealed type and every permitted subclass must be in the same package.
| Project type | Where a permitted subclass may live |
|---|---|
| Named module | Anywhere in the same named module |
| Unnamed module | In the same package |
| Same compilation unit | `permits` may sometimes be omitted |
A compilation unit is normally one Java source file. The module rule still applies when the classes are declared in the same file.
An explicit `permits` clause across files
With an explicit clause, each direct subclass can have its own source file. In an unnamed module, all files must still use the same package declaration.
For example, these files can belong to the same package:
// Message.java
package messaging;
sealed interface Message permits Email, Sms {}// Email.java
package messaging;
final class Email implements Message {}// Sms.java
package messaging;
record Sms(String text) implements Message {}Email and Sms are direct permitted subclasses of Message. The record is implicitly final, so it satisfies the requirement for a permitted subclass.
If these files use different packages without a named module, compilation fails. If the project uses a named module, the packages may differ, but all three types must belong to that module.
Omitting `permits`
You may omit the clause when every direct subclass is declared in the same compilation unit as the sealed type. The compiler then infers the permitted subclasses from that source file.
sealed interface InlineCommand {
record Start() implements InlineCommand {}
record Stop() implements InlineCommand {}
}
public class Main {
public static void main(String[] args) {
InlineCommand command = new InlineCommand.Start();
System.out.println(command.getClass().getSimpleName());
}
}Save this as Main.java and compile it with Java 17 or later:
javac --release 17 Main.java
java MainThe nested records are declared inside InlineCommand, and therefore in the same compilation unit. No permits clause is needed.
You can also place package-private top-level classes in the same source file:
sealed interface Token {}
final class NumberToken implements Token {}
final class EndToken implements Token {}If these are later moved into separate files, add an explicit clause:
sealed interface Token permits NumberToken, EndToken {}Common mistakes
- Putting a permitted subclass in another package when the types are in the unnamed module.
- Forgetting that a named module means the same module, not necessarily the same package.
- Omitting
permitswhile placing a direct subclass in another source file. - Adding a new subclass to a hierarchy whose inferred permitted list came from one source file.
- Forgetting that every permitted class must be
final,sealed, ornon-sealed.
Use an explicit permits clause when the hierarchy is intentionally spread across files or packages. Omit it when keeping a small hierarchy together in one source file improves readability.
Exercise
In an unnamed module, can a sealed interface in package orders permit a class in package orders.internal?
Show answer
No. In the unnamed module, the sealed interface and every permitted subclass must be in the same package. Use the same package or place both types in a named module.