Cosmic Module
J
Qubits of DPK
March 14, 2026
Core Java
Layman Explanation
Normally any class can extend your class. Sealed classes let you say: "ONLY these specific classes are allowed to extend me. Nobody else."
Like a VIP club with a fixed, known guest list.
Real World Analogy
A payment system: only Credit Card, Debit Card, and UPI are valid payment types. No one should be able to add "BitcoinPayment" or "InvalidPayment" as a new type randomly. Sealed classes enforce this.
What is a Sealed Class?
A class that restricts which other classes can extend it using the permits clause.
java
QUBITS OF DPK
Three Options for Permitted Subclasses
java
QUBITS OF DPK
Sealed Interfaces
java
QUBITS OF DPK
Power: Sealed Classes + Pattern Matching
java
QUBITS OF DPK
Production Use Cases
java
QUBITS OF DPK
️ All Traps
java
QUBITS OF DPK
30-Second Interview Answer
"Sealed classes introduced in Java 17 restrict which classes can extend them using the permits clause. Each permitted subclass must be declared as final (no further extension), sealed (restricted extension), or non-sealed (open). This gives developers control over class hierarchies and enables exhaustive pattern matching in switch without a default case since the compiler knows all possible subtypes. Perfect for domain modeling where a fixed set of types is needed, like payment types, API responses, or order states."
Interview Questions & MAANG-Level Answers
Q1. What is a sealed class in Java?
Introduced in Java 17 (JEP 409), a sealed class restricts which other classes can extend it using the permits clause: public sealed class Shape permits Circle, Rectangle, Triangle. Only those listed classes can extend Shape. Any other class trying to extend Shape gets a compile error. Sealed interfaces work the same way. This gives developers explicit control over class hierarchies.
Q2. What are the three options a permitted subclass must choose from?
Every class in the permits list MUST declare one of: (1) final — cannot be extended further. final class Circle extends Shape {}. (2) sealed — can be extended but with its own restricted permits list. sealed class Quadrilateral extends Shape permits Square, Rectangle {}. (3) non-sealed — opens the hierarchy back up to anyone. non-sealed class FreeformShape extends Shape {} — anyone can extend FreeformShape. This is a deliberate escape hatch.
Q3. What is the benefit of sealed classes with pattern matching?
Since the compiler knows ALL possible subtypes of a sealed class, switch expressions can be exhaustive without a default case:
java
QUBITS OF DPK
If you add a new permitted subclass (e.g., Pentagon), the compiler flags all switch expressions that need updating — compile-time safety for exhaustive type dispatch.
Q4. What is the difference between sealed and final?
final class: cannot be extended by ANYONE — complete prevention of subclassing. sealed class: can be extended, but ONLY by the explicitly listed permitted classes. Sealed allows a controlled, known set of extensions; final allows none. final is a complete door-lock; sealed is a VIP list.
Q5. Where must permitted subclasses be located?
In Java 17+, permitted subclasses must be in the same package (or same module) as the sealed class. They don't need to be in the same file (though they can be, as unnamed/nested classes). The compiler verifies this at compile time. For single-file sealed hierarchies (e.g., in examples), all classes can be in the same file.