sealed type
A class or interface that lists exactly which types may extend or implement it, so the compiler knows the complete family.
In more detail
The permits clause names the direct subtypes, and each must declare itself final, sealed or non-sealed. Because the family is closed, a switch over it can be exhaustive with no default branch, and adding a new subtype makes the compiler point at every switch that must handle it. Sealed types have been standard since Java 17.
Example
public sealed interface PaymentResult permits Approved, Declined, Pending { }Related terms
- exhaustive switch — A switch that the compiler has proven handles every possible value, so it needs no default branch.
- switch expression — A switch construct that produces a value, with one arm per case and no fall-through between them.
- record (Java) — A compact Java class for plain data, declared with its components in one line, for which the compiler generates the fields, accessors, equals, hashCode and toString.
- final — A Java keyword meaning cannot be changed further: a final field is assigned once, and a final class cannot be extended.
- pattern matching — Testing a value against a shape, such as a type, a range or the values of its properties, and optionally extracting parts of it in the same step.
Taught in
- Sealed Types And Exhaustiveness — Modern Java: Virtual Threads & High-Throughput Services
- Pattern Matching And Text Blocks — Modern Java: Virtual Threads & High-Throughput Services
- Inheritance, Overriding And Object's Methods — Oracle Certified Professional: Java SE 21 Developer (1Z0-830) Exam Prep
- Sealed Types, Casts And Pattern Matching — Oracle Certified Professional: Java SE 21 Developer (1Z0-830) Exam Prep
- Sprint: Object-Oriented Java — Oracle Certified Professional: Java SE 21 Developer (1Z0-830) Exam Prep
- Mock Exam: Sit It, Score It — Oracle Certified Professional: Java SE 21 Developer (1Z0-830) Exam Prep