A sealed class is a special type of class introduced in Java 15 that allows developers to explicitly control which classes or interfaces can inherit from it. By restricting inheritance to a predefined set of subclasses, sealed classes provide better control over class hierarchies, improve maintainability, and enhance type safety.
Consider a payment system where only CreditCardPayment, UpiPayment, and NetBankingPayment should be valid payment types. A sealed class ensures that no other unauthorized payment implementation can be created outside the defined hierarchy.
sealed class Payment
permits CreditCardPayment,
UpiPayment,
NetBankingPayment {
}
final class CreditCardPayment
extends Payment {
}
final class UpiPayment
extends Payment {
}
final class NetBankingPayment
extends Payment {
}
public class Main {
public static void main(String[] args) {
Payment payment =
new UpiPayment();
System.out.println(
payment.getClass().getSimpleName());
}
}A concise interview answer is: A sealed class restricts which classes can extend or implement it by explicitly defining permitted subclasses. It helps create controlled class hierarchies, improves type safety, and is particularly useful when all valid subtypes are known in advance.
Last updated: 11 November 2026
Previous question: What is a record in Java, and when should you use it?
Next question: How do you implement the producer-consumer problem using wait() and notify()?