Sealed Class - Java
Sealed classes were introduced recently in java version 15 which was released on
September 15th, 2020. Explaining it in simple terms, it is used in inheritance and its
main purpose is to give the user the ability to allow exclusive access to one or more
specified sub-classes.
For example - There's a superclass named "cars" and there are subclasses named
"Ford", "BMW", "Cup", "Mercedes", "Table". Now technically speaking, all 5 classes
should be able to inherit the characteristics of the superclass "cars". But thinking
realistically, we know that cups and tables have no relation with cars in real life, they are
normal household objects. So ideally, only subclasses named "BMW", "Mercedes", and
"Ford" should be able to inherit the properties of "cars”. Sealed classes enforce this rule
by permitting only the specified classes to inherit the properties.
public sealed class cars permits BMW, Mercedes, Ford
{
. . .
}
Why should we use sealed classes when we can simply allow all classes to inherit the
properties of the superclass with minor changes, and save the need for extra lines of
code?
Well, you know that Java is all about object-oriented programming. OOPS helps in
reusability, inheriting properties from different classes without the need to define extra
properties for subclasses, but its main purpose is to provide a clear structure for the
program making the whole code easier to understand and maintain. Many Java
programmers simply try to reuse the code as much as possible to reduce the no. of
lines in the code, for eg. A class named "Lion" inheriting the properties of "cars". OOPS
associates the code to real-life entities to make it easier to maintain.
To explain it simply, let's take a scenario. Imagine if a developer writes a code without
using sealed classes, there will be random classes inheriting a superclass’s properties
to make the code short. But while maintaining larger codebases, any person/client will
be confused if there are classes like "table" or "fans" inheriting the properties of class
"cars". This will make it harder to maintain the code and the maintainer will have a tough
job reading and understanding the whole code unless he goes through the whole code
from starting to understand the inheritance tree, which is a very time-consuming task.
But now, if we use sealed class and permit specific classes for inheritance then any
user or maintainer can easily get the idea, which subclass is inheriting the properties of
a superclass making it easier to modify or maintain, thus consuming less time. Overall,
the main point of using a sealed class is to make the code relatable to real life as much
as possible. This also allows developers to restrict clients or other developers to declare
any more primitives.
Are sealed classes the same as final classes?
No, they are very different, and sealed class's goal is not to change or act as final in any
way. Final simply restricts any class to be inherited at all while sealed restricts only
those classes that are not specified.
public sealed abstract class cars permits BMW, Mercedes, Ford
{
non-sealed class BMW extends cars{}
Non-sealed class Mercedes extends cars{}
Non-sealed class Ford extends cars{}
final class tires extends BMW
. . .
}
In the above snippet,
1. Final class "tires" cannot be inherited by any other subclass.
2. Sealed class "cars" will only allow subclasses, "Mercedes", "BMW" and "Ford" to
inherit its properties while restricting any other class.
An important thing to note while using sealed classes is that you will have to forcefully
define an inherited class as "non-sealed", “sealed”, or “final”.
In the snippet above, the subclasses inheriting the properties of "cars" are either
specific as sealed, non-sealed, or final. Not specifying a subclass will result in
compilation errors.
An example of sealed class syntax and usage-
public abstract sealed class Vehicle permits Car, Truck {
protected final String registrationNumber;
public Vehicle(String registrationNumber) {
this.registrationNumber = registrationNumber;
}
public String getRegistrationNumber() {
return registrationNumber;
}
}
Notes/Summary
1. Sealed classes allow only specified subclasses to inherit the properties of the
superclass.
2. Its main purpose is to relate the code with real-life entities and make it easier to
understand.
3. Makes maintenance easy for clients or co-developers.
4. It's different from the final class.
Final makes the class non-inheritable whereas sealed makes the class
selectively inheritable to specified subclasses.
5. You need to forcefully specify the subclasses as either “non-sealed”, “sealed”, or
“final”. Otherwise, it will give a compilation error.
6. You can only use specify an interface as sealed or non-sealed. Final is default in
interface.

Sealed classes java

  • 1.
    Sealed Class -Java Sealed classes were introduced recently in java version 15 which was released on September 15th, 2020. Explaining it in simple terms, it is used in inheritance and its main purpose is to give the user the ability to allow exclusive access to one or more specified sub-classes. For example - There's a superclass named "cars" and there are subclasses named "Ford", "BMW", "Cup", "Mercedes", "Table". Now technically speaking, all 5 classes should be able to inherit the characteristics of the superclass "cars". But thinking realistically, we know that cups and tables have no relation with cars in real life, they are normal household objects. So ideally, only subclasses named "BMW", "Mercedes", and "Ford" should be able to inherit the properties of "cars”. Sealed classes enforce this rule by permitting only the specified classes to inherit the properties. public sealed class cars permits BMW, Mercedes, Ford { . . . } Why should we use sealed classes when we can simply allow all classes to inherit the properties of the superclass with minor changes, and save the need for extra lines of code? Well, you know that Java is all about object-oriented programming. OOPS helps in reusability, inheriting properties from different classes without the need to define extra properties for subclasses, but its main purpose is to provide a clear structure for the program making the whole code easier to understand and maintain. Many Java programmers simply try to reuse the code as much as possible to reduce the no. of lines in the code, for eg. A class named "Lion" inheriting the properties of "cars". OOPS associates the code to real-life entities to make it easier to maintain. To explain it simply, let's take a scenario. Imagine if a developer writes a code without using sealed classes, there will be random classes inheriting a superclass’s properties to make the code short. But while maintaining larger codebases, any person/client will be confused if there are classes like "table" or "fans" inheriting the properties of class "cars". This will make it harder to maintain the code and the maintainer will have a tough job reading and understanding the whole code unless he goes through the whole code from starting to understand the inheritance tree, which is a very time-consuming task.
  • 2.
    But now, ifwe use sealed class and permit specific classes for inheritance then any user or maintainer can easily get the idea, which subclass is inheriting the properties of a superclass making it easier to modify or maintain, thus consuming less time. Overall, the main point of using a sealed class is to make the code relatable to real life as much as possible. This also allows developers to restrict clients or other developers to declare any more primitives. Are sealed classes the same as final classes? No, they are very different, and sealed class's goal is not to change or act as final in any way. Final simply restricts any class to be inherited at all while sealed restricts only those classes that are not specified. public sealed abstract class cars permits BMW, Mercedes, Ford { non-sealed class BMW extends cars{} Non-sealed class Mercedes extends cars{} Non-sealed class Ford extends cars{} final class tires extends BMW . . . } In the above snippet, 1. Final class "tires" cannot be inherited by any other subclass. 2. Sealed class "cars" will only allow subclasses, "Mercedes", "BMW" and "Ford" to inherit its properties while restricting any other class. An important thing to note while using sealed classes is that you will have to forcefully define an inherited class as "non-sealed", “sealed”, or “final”. In the snippet above, the subclasses inheriting the properties of "cars" are either specific as sealed, non-sealed, or final. Not specifying a subclass will result in compilation errors.
  • 3.
    An example ofsealed class syntax and usage- public abstract sealed class Vehicle permits Car, Truck { protected final String registrationNumber; public Vehicle(String registrationNumber) { this.registrationNumber = registrationNumber; } public String getRegistrationNumber() { return registrationNumber; } } Notes/Summary 1. Sealed classes allow only specified subclasses to inherit the properties of the superclass. 2. Its main purpose is to relate the code with real-life entities and make it easier to understand. 3. Makes maintenance easy for clients or co-developers. 4. It's different from the final class. Final makes the class non-inheritable whereas sealed makes the class selectively inheritable to specified subclasses. 5. You need to forcefully specify the subclasses as either “non-sealed”, “sealed”, or “final”. Otherwise, it will give a compilation error. 6. You can only use specify an interface as sealed or non-sealed. Final is default in interface.