Are Java 17 Sealed Classes a Game-Changer for Inheritance Control?
Introduction:-
Sealed classes are a feature introduced in Java 16 (as a preview feature) and finalized in Java 17.
In this complete blog tutorial, we'll see what sealed classes are and why they're useful. But first, let's start with the basic definition of a sealed class.
Sealed class:-
"Sealed class offer a powerful mechanism to restrict the inheritance hierarchy. "
Which means, When you want's inheritance but not all classes can do it, their should be a limited classes which you want's to be inherited. then you can do this by using sealed class.
Declaration of sealed class is not much complicated. simply you just need to add the
sealed
keyword to it's declaration.After the class declaration add
permits clause
to give the permission to those sub-classes which you want's to be inherited.Only permitted classes can have the access to extends the sealed class.
Syntax :-
public sealed class Class_Name permits Subclass1, Subclass2, ... {
// Class members and methods
}
ex :-
sealed class Color permits Red, Green, Blue {
public void display() {
System.out.print("This is Color class");
}
}
- in the above example we can see that the
Color
class is a sealed class that give the permission toRed
&Green
classes to extends it. If any other class tries to extend it, then it will give a compiler error. - A class that extends a sealed class must have either
final
,sealed
ornon-sealed
keyword in its declaration.
1) sealed :-
sealed class can only be extended by it's permitted sub-classes.
sealed class Red extends Color permits R {
public void display() {
System.out.println("This is Red class");
}
}
The code defines a sealed class "Red" extending it's super-class "Color" and permitting only class "R", with a display method printing "This is Red class".
final class R extends Red{
public void display() {
System.out.println("This is R class");
}
}
The code defines a final class "R" that extends the sealed class "Red" and overrides the display method to print "This is R class".
2) non-sealed :-
non-sealed class can be extends by any sub-class. a sealed class cannot prevent its permitted sub-classes from doing this.
non-sealed class Green extends Color {
public void display() {
System.out.println("This is Green class");
}
}
The code defines a non-sealed class "Green" that extends it's super-class "Color" and contains a display method which prints "This is Green class".
3) final :-
final class can't be inherited further.
final class Blue extends Color {
public void display() {
System.out.println("This is Blue class");
}
}
The code defines a final class "Blue" that extends it's super-class "Color" and contains a display method which prints "This is Blue class". This means that the class "Blue" cannot be subclassed further.
Code Execution
public class Main
{
public static void main(String[] args)
{
Color obj1 = new Red();
Color obj2 = new Green();
Color obj3 = new Blue();
obj1.display();
obj2.display();
obj3.display();
Red r = new R();
r.display();
}
}
Output :-
This is Red class
This is Green class
This is Blue class
This is R class
features
Sealed classes helps in creating a finite set of classes in inheritance.
Sealed classes specify which classes can extend them, providing a clear hierarchy.
Sealed classes ensure that only permitted sub-classes are created, avoiding unexpected behavior.
Useful for creating frameworks, allowing developers to dictate which classes users can extend.
Especially beneficial in large projects, it prevents unintentional changes to important classes.
Note:-
- Sealed class must specify it's permitted sub-class using permits clause.
- Sealed class must have sub-classes.
- Only the listed sub-classes are allowed to extend the sealed class.
- Permitted sub-classes must be in same package.
- Permitted sub-classes should extends their sealed super class directly.
conclusion
sealed classes in Java provide a powerful tool for controlling class hierarchies. They allow us to explicitly specify which classes can extend a particular class, offering a clear and organized structure for our code. This feature is valuable in preventing unintended subclasses and maintaining code integrity.
Thanks for reading! Hopefully, this article helps you to understand sealed class in Java17. Please submit any suggestions or comments you may have in the comment section of this article and don't forget to hit the ❤️ like.
Thank you.😀
Top comments (1)
Although this article explain sealed classes very well but i would appreciate it you gave a broader explanation about can'ts and cans.