Important Concepts of Sealed Classes
   1. Sealed Class Declaration:
- A class is declared with the sealed modifier.
- It must specify the permitted subclasses using the permits clause.
- Example:
public sealed class Shape permits Circle, Rectangle, Triangle {
// Class body
}
Code language: PHP (php)
  2.Permitted Subclasses:
- The permits clause lists the classes that are allowed to extend the sealed class.
- These subclasses must be in the same module (or package if not using modules).
- Subclasses must be explicitly declared as final, sealed, or non-sealed:
- final: Cannot be extended further.
- sealed: Can only be extended by its own permitted subclasses.
- non-sealed: Can be extended freely (removes restrictions).
public final class Circle extends Shape {
// Circle implementation
}
public sealed class Rectangle extends Shape permits Square {
// Rectangle implementation
}
public final class Square extends Rectangle {
// Square implementation
}
public non-sealed class Triangle extends Shape {
// Triangle implementation
}
Code language: PHP (php)
  3.Rules:
- A sealed class cannot be extended by any class not listed in the permits clause.
- If the permits clause is omitted, the compiler infers permitted subclasses from those in the same compilation unit.
- All permitted subclasses must directly extend the sealed class.
  4.Use with Records:
Sealed classes pair well with records (introduced in Java 16) for concise, immutable data hierarchies:
public sealed interface Vehicle permits Car, Truck {
String getName();
}
public record Car(String name) implements Vehicle { }
public record Truck(String name) implements Vehicle { }
Code language: PHP (php)
Restrictions
- Sealed classes cannot be instantiated directly if they are abstract.
- The permits clause is mandatory unless subclasses are in the same file and can be inferred.
- Sealed interfaces are also supported, with similar rules.
Modifier | Meaning |
---|---|
sealed | restrict subclassing |
final | no subclassing allowed |
non-sealed | subclassing is open again |
Example Use Case
// Sealed superclass
sealed class Vehicle
permits Car, Truck, Bike {
public void displayType() {
System.out.println("I am a Vehicle");
}
}
// Final subclass
final class Car extends Vehicle {
public void carInfo() {
System.out.println("I am a Car");
}
}
// Sealed subclass with its own permits
sealed class Truck extends Vehicle
permits PickupTruck {
public void truckInfo() {
System.out.println("I am a Truck");
}
}
// A subclass of Truck
final class PickupTruck extends Truck {
public void pickupInfo() {
System.out.println("I am a Pickup Truck");
}
}
// Non-sealed subclass
non-sealed class Bike extends Vehicle {
public void bikeInfo() {
System.out.println("I am a Bike");
}
}
// Main Class
public class SealedClassDemo {
public static void main(String[] args) {
Car car = new Car();
car.displayType();
car.carInfo();
PickupTruck pickup = new PickupTruck();
pickup.displayType();
pickup.truckInfo();
pickup.pickupInfo();
Bike bike = new Bike();
bike.displayType();
bike.bikeInfo();
}
}
/*
I am a Vehicle
I am a Car
I am a Vehicle
I am a Truck
I am a Pickup Truck
I am a Vehicle
I am a Bike
*/
Code language: PHP (php)
Another Example Use case
Software Engineer Designation Hierarchy (using Java 17 Sealed Classes)
// Sealed superclass
sealed class SoftwareEngineer
permits Intern, Developer, Architect {
public void showRole() {
System.out.println("I am a Software Engineer.");
}
}
// Final subclass - No further subclassing allowed
final class Intern extends SoftwareEngineer {
public void showInternInfo() {
System.out.println("I am an Intern, learning and growing!");
}
}
// Sealed subclass - with controlled further inheritance
sealed class Developer extends SoftwareEngineer
permits JuniorDeveloper, SeniorDeveloper {
public void showDeveloperInfo() {
System.out.println("I am a Developer, writing and maintaining code!");
}
}
// Subclass of Developer - Junior Developer
final class JuniorDeveloper extends Developer {
public void showJuniorDevInfo() {
System.out.println("I am a Junior Developer, focusing on simple tasks.");
}
}
// Subclass of Developer - Senior Developer
final class SeniorDeveloper extends Developer {
public void showSeniorDevInfo() {
System.out.println("I am a Senior Developer, designing complex systems.");
}
}
// Non-sealed subclass - Architect - anyone can subclass it later
non-sealed class Architect extends SoftwareEngineer {
public void showArchitectInfo() {
System.out.println("I am an Architect, creating system architecture!");
}
}
// Main Class
public class SealedDesignationDemo {
public static void main(String[] args) {
Intern intern = new Intern();
intern.showRole();
intern.showInternInfo();
JuniorDeveloper juniorDev = new JuniorDeveloper();
juniorDev.showRole();
juniorDev.showDeveloperInfo();
juniorDev.showJuniorDevInfo();
SeniorDeveloper seniorDev = new SeniorDeveloper();
seniorDev.showRole();
seniorDev.showDeveloperInfo();
seniorDev.showSeniorDevInfo();
Architect architect = new Architect();
architect.showRole();
architect.showArchitectInfo();
}
}
/*
I am a Software Engineer.
I am an Intern, learning and growing!
I am a Software Engineer.
I am a Developer, writing and maintaining code!
I am a Junior Developer, focusing on simple tasks.
I am a Software Engineer.
I am a Developer, writing and maintaining code!
I am a Senior Developer, designing complex systems.
I am a Software Engineer.
I am an Architect, creating system architecture
*/
Code language: PHP (php)
Advantages
Advantage | Meaning |
---|---|
Controlled Inheritance | Only permitted classes can extend |
Better Security | Protects against unauthorized subclassing |
Clear Design | Easy to understand the allowed hierarchy |
Safe Refactoring | Easy and safe to change sealed classes |
Compiler Optimization | Helps in pattern matching and switch |
Early Error Detection | Catches mistakes at compile time |
Flexibility | non-sealed allows open extensions if needed |
Sealed classes are like VIP entry passes in object-oriented design ,
only authorized people can enter, and the security guards (compiler) ensure that!
Sealed classes in Java 17 bring a powerful way to control inheritance, enhance security, and design clean hierarchies.
By explicitly stating which classes are permitted to extend or implement a sealed class, developers can prevent unintended subclassing, make code safer, and improve readability.
Sealed classes work perfectly for systems where certain roles, behaviors, or types must be strictly defined and enforced like roles in a company, types of bank accounts, or various states in a game.
In addition, sealed classes help compilers perform better optimization, enable smarter pattern matching, and allow developers to catch mistakes early at compile-time.