Clean • Professional
Sealed Classes were introduced as a preview feature in Java 15 and later became a standard feature in Java 17. They are designed to give developers better control over inheritance in Java.
With sealed classes, you can explicitly define which classes are allowed to extend or implement a particular class, making your code more secure and predictable.
A sealed class is a special type of class in Java that restricts which other classes can extend it. Only the classes that are explicitly permitted are allowed to inherit from it.
In simple words: A sealed class lets you control and restrict inheritance so only selected classes can extend it.
Syntax of Sealed Class
sealed class Shape permits Circle, Rectangle {
}
👉 This means only Circle and Rectangle are allowed to extend the Shape class.

Before sealed classes, any class could be extended freely in Java. This means there was no restriction on inheritance, which often caused design and maintenance issues in large applications.
Before sealed classes:
class Animal {}
class Dog extends Animal {}
class Cat extends Animal {}
class Tiger extends Animal {}
Problems:
Sealed classes were introduced to solve the problems of uncontrolled inheritance in Java. They allow developers to explicitly define which classes can extend a parent class, making the system more structured and secure.
Key Benefits
The permits keyword is used in sealed classes to define which subclasses are allowed to extend the parent class. It gives explicit control over inheritance and ensures only approved classes can inherit.
sealed class Shape permits Circle, Rectangle {
}
👉 This means only Circle and Rectangle are allowed to extend the Shape class.
Sealed classes allow only specific subclasses to extend them. Each permitted subclass must explicitly follow Java’s rules like final, sealed, or non-sealed.
Step 1: Sealed Class
sealed class Animal permits Dog, Cat {
}
Step 2: Subclasses
final class Dog extends Animal {
}
final class Cat extends Animal {
}
Sealed classes in Java follow some strict rules to ensure controlled and safe inheritance. These rules define how a class can be extended and how subclasses should behave.
permitsA sealed class must explicitly specify which classes are allowed to extend it using the permits keyword.
sealed class Shape permits Circle, Square {
}
👉 This ensures only listed classes can inherit the sealed class.
Every subclass of a sealed class must declare its inheritance type clearly:
sealed class Shape permits Circle, Square {
}
final class Circle extends Shape {
}
non-sealed class Square extends Shape {
}
Explanation:
Circle is final, so no further inheritance is allowedSquare is non-sealed, so it can be extended by any classAll permitted subclasses must be defined in the same module or package as the sealed class. This ensures better encapsulation and control.
package shapes;
sealed class Shape permits Circle {
}
final class Circle extends Shape {
}
👉 If a subclass is outside the allowed package/module, Java will throw a compilation error.
In sealed classes, every permitted subclass must define how inheritance will behave further. Java provides three different types of subclasses to control this behavior.
final subclassA final subclass cannot be extended further. Once a class is declared final, it becomes the end of the inheritance chain.
final class Dog extends Animal {
}
👉 This means no other class can extend Dog.
sealed subclassA sealed subclass continues to restrict inheritance by specifying which classes are allowed to extend it.
sealed class Cat extends Animal permits Persian {
}
👉 Only Persian class is allowed to extend Cat.
non-sealed subclassA non-sealed subclass becomes open for inheritance, meaning any class can extend it freely.
non-sealed class Tiger extends Animal {
}
👉 This removes restrictions and allows unlimited inheritance.

The sealed feature in Java is not limited to classes only — it can also be applied to interfaces. This allows you to control which classes are allowed to implement the interface.
Example
sealed interface Vehicle permits Car, Bike {
}
final class Car implements Vehicle {
}
final class Bike implements Vehicle {
}
👉 In this example, only Car and Bike are allowed to implement the Vehicle interface.
Sealed classes are very useful in real-world applications where you need to restrict and control behavior, such as payment systems.
Payment System Example
sealed class Payment permits CardPayment, UPIPayment {
}
final class CardPayment extends Payment {
}
final class UPIPayment extends Payment {
}

👉 In this example, only CardPayment and UPIPayment are allowed as valid payment methods. No other class can extend Payment, which ensures controlled and secure design.
Sealed classes provide better control over inheritance, making Java applications more structured and secure. They are especially useful in large and complex systems where class hierarchy needs to be strictly managed.
Although powerful, sealed classes are not always suitable for every situation.
| Feature | Sealed Class | Normal Class |
|---|---|---|
| Inheritance | Restricted (controlled by permits) | Open (any class can extend) |
| Control | High (explicit hierarchy control) | Low (no restrictions) |
| Flexibility | Medium (limited to allowed classes) | High (fully flexible) |
| Use Case | Enterprise systems, domain modeling | General-purpose programming |
| Feature | Normal Class | Final Class | Sealed Class |
|---|---|---|---|
| Definition | A regular class with no inheritance restrictions | A class that cannot be extended by any other class | A class that allows only specific classes to extend it |
| Inheritance | Open for all classes | Not allowed | Restricted to permitted classes only |
| Control Over Hierarchy | No control | Complete restriction | Controlled and limited |
| Flexibility | High flexibility | No flexibility | Moderate flexibility |
| Security | Low | High | High |
| Keyword Used | No keyword | final | sealed with permits |
| Subclass Rules | No restrictions | No subclasses allowed | Subclasses must be declared as final, sealed, or non-sealed |
| Extendable Further | Yes | No | Depends on subclass type |
| Use Case | General purpose development | Prevent inheritance, immutable design | Controlled class hierarchy and domain modeling |
| Example | class A {} | final class A {} | sealed class A permits B, C {} |
| Java Version | Available in all versions | Available in all versions | Introduced in Java 15 and stable in Java 17 |

Sealed classes are widely used in real-world applications where you need controlled inheritance and strict system design. They help in building safe and predictable architectures.
Sealed classes are very useful in systems where the types are fixed, such as notification services. In this example, we define a limited set of notification types that are allowed.
Sealed Class
sealed class Notification permits Email, SMS, Push {
}
Subclasses
final class Email extends Notification {
}
final class SMS extends Notification {
}
non-sealed class Push extends Notification {
}
here:
Email and SMS are fixed and cannot be extended furtherPush is open for extension if neededSealed classes are useful when you want strict control over inheritance and a well-defined class structure. They are mainly designed for large and structured applications.
Sealed classes are not suitable for every scenario, especially when flexibility is more important than strict structure.
Sealed Classes in Java provide a powerful way to control inheritance and design more predictable class hierarchies.
They are especially useful in modern Java applications where structure, safety, and maintainability are important.
👉 Overall, sealed classes are an important feature of modern Java (Java 17+), widely used in enterprise-level applications and domain-driven design.