Quick revision
The short answer
A sealed class or interface restricts which classes may directly extend or implement it, for example sealed interface Shape permits Circle, Square {}. Every permitted subclass must declare itself final, sealed or non-sealed, and must live in the same module as the sealed type, or in the same package if the code is in the unnamed module. The permits clause can be left out when all subclasses are in the same source file. The feature is final since Java 17 (JEP 409), and records and enums, being implicitly final, fit naturally as permitted subtypes.
What to remember
In this answer, we declared sealed interface Shape permits Circle, Square and saw that only listed types may extend it directly. Each permitted subclass must be final, sealed or non-sealed, and must be in the same module, or the same package in the unnamed module. The permits clause can be dropped when everything is in one source file. Sealing has been final since Java 17 through JEP 409, it is enforced by both compiler and JVM, and records and enums fit naturally as permitted subtypes, enabling exhaustive switches.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
A sealed class or interface restricts which classes may directly extend or implement it, for example sealed interface Shape permits Circle, Square {}. Every permitted subclass must declare itself final, sealed or non-sealed, and must live in the same module as the sealed type, or in the same package if the code is in the unnamed module. The permits clause can be left out when all subclasses are in the same source file. The feature is final since Java 17 (JEP 409), and records and enums, being implicitly final, fit naturally as permitted subtypes.
Why this question is asked
Before Java 17, a class hierarchy was either open to anyone or closed with the final keyword, nothing in between. Sealed types add the in-between: a hierarchy that the author can extend but nobody else can. Interviewers ask about the syntax and rules because they are short to state but easy to get wrong in practice, and because sealing is what makes exhaustive switch over types possible. In this answer, we will write a sealed interface, look at the three modifiers a permitted subclass must choose from, see where subclasses are allowed to live, look at the short form without permits, and see why records fit so well.
1Declaring a Sealed Interface
We add the sealed modifier to a class or interface and list the allowed direct subtypes in a permits clause. Only those types may extend or implement it directly.
Notice what sealing does not mean. Shape is still a normal interface: we can have variables of type Shape, write methods that take a Shape, and call its methods. The restriction is only on who may declare themselves a direct subtype. The compiler records the list in the class file in a PermittedSubclasses attribute, and the JVM checks it when it loads classes, so a subclass compiled separately cannot sneak in.
2What Each Permitted Subclass Must Declare
A sealed type controls its direct subtypes, but each of those subtypes must say what happens below it. It must use exactly one of three modifiers. final means no further subclasses. sealed means it continues the restriction with its own permits list. non-sealed means it reopens the hierarchy, so anyone may extend it.
- sealed abstract class Vehiclepermits Car, Truck, Bike
- final class Carno subclasses allowed
- sealed class Truck permits Tipperrestriction continues one level down
- non-sealed class Bikereopened: EBike and anyone else may extend it
4 layers, from the top down. Step through them one at a time.
Every permitted subtype chooses final, sealed or non-sealed.
The reason the language insists on an explicit choice is that the author of the sealed type is promising readers, and the compiler, a complete list of direct variants. Each variant must then state clearly whether that promise continues below it. non-sealed is deliberately written as a visible keyword, so opening a branch is a conscious decision that shows up in review.
3Where Permitted Subclasses Must Live
The sealed type and its permitted subclasses must be close together, so the hierarchy is owned and compiled as one unit. If the code is in a named module, one with a module-info.java, the permitted subclasses must be in the same module, but they may be in different packages of that module. If the code is in the unnamed module, which is the case for ordinary classpath applications, including most Spring Boot applications, they must be in the same package.
In practice this means a sealed type cannot be extended by another team's code that depends on our JAR, which is exactly the point: the variants are part of our API, and we are the only ones who can add one. It also means that if we try to permit a class from another module or package, the compiler reports an error at the sealed type.
4Leaving Out the permits Clause
When the sealed type and all its permitted subclasses are declared in the same source file, we may omit permits, and the compiler infers the list from the subclasses it finds in that file. This is common for small result types, where the subtypes are nested inside the interface.
The switch shows the main reason sealed types exist. Because the compiler knows Ok and Failed are the only possible Results, it accepts a switch that covers them both without a default branch, and it reports an error if a case is missing. If we later add a third variant, every such switch stops compiling until it handles the new case.
5Why Records and Enums Fit So Well
Records are implicitly final, so they satisfy the modifier rule without writing anything, and they are transparent data carriers, so each variant is just a named group of values that record patterns can take apart. Enums are implicitly final too, unless a constant has a body, in which case the enum is implicitly sealed, and an enum can implement a sealed interface. A sealed interface with record subtypes is the standard Java way to model a closed set of variants with different data, such as payment methods, commands or the outcomes of an operation. Sealed classes were previewed in Java 15 and 16 and became final in Java 17 through JEP 409.
6Follow-up questions
Can a sealed interface be implemented by a lambda?
No. A lambda would be an anonymous implementation that is not in the permits list, so sealed functional interfaces cannot be targeted by lambdas or anonymous classes. Sealed interfaces are for closed sets of named variants, not for callbacks.
Does the JVM enforce sealing, or only the compiler?
Both. The compiler checks the source, and the class file carries a PermittedSubclasses attribute that the JVM checks when loading a subclass. A class compiled elsewhere that claims to extend a sealed type it is not permitted to extend fails to load with an IncompatibleClassChangeError.
Can permitted subclasses be in different files?
Yes, that is the normal case for larger hierarchies. Then the permits clause is required, and the subclasses must be in the same module, or the same package for the unnamed module.
7Conclusion
In this answer, we declared sealed interface Shape permits Circle, Square and saw that only listed types may extend it directly. Each permitted subclass must be final, sealed or non-sealed, and must be in the same module, or the same package in the unnamed module. The permits clause can be dropped when everything is in one source file. Sealing has been final since Java 17 through JEP 409, it is enforced by both compiler and JVM, and records and enums fit naturally as permitted subtypes, enabling exhaustive switches.
Open with
A sealed class or interface lets the author say exactly which classes may directly extend or implement it. The syntax is sealed interface Shape permits Circle, Square, and any other class that tries to implement Shape gets a compile error.
Then, if they want more
The type is still used normally; only the list of direct subtypes is restricted, and the JVM enforces it as well through the PermittedSubclasses attribute. Every permitted subclass must declare one of three modifiers: final, so nothing extends it, sealed with its own permits list, so the restriction continues, or non-sealed, which reopens that branch for anyone.
The keyword has to be explicit so that opening a hierarchy is a visible decision. Permitted subclasses must live close to the sealed type: in the same module if we use named modules, or in the same package if we are on the classpath in the unnamed module, which is typical for Spring Boot apps.
If all subclasses are in the same source file, for example records nested inside the interface, I can leave out the permits clause and the compiler infers it. Sealed classes are final since Java 17, JEP 409.
Records fit very well as permitted subtypes because they are implicitly final and are plain data, so a sealed interface with record variants gives exhaustive switches with record patterns and no default branch, and adding a new variant makes the compiler point at every switch that needs updating.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 17, Java 15
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- The Java Language Specification, Java SE 21, Java SE 21 API documentation
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Explain sealed classes and interfaces: syntax, what permitted subclasses must declare...": https://scrivio-interview-library.netlify.app/java/sealed-and-pattern-matching/d3f5d665be/