In java 17, a new type of classes and interfaces are introduced to have a control over the inheritance. They are sealed classes and sealed interfaces. Using this feature, you can control which class can extend or implement a particular class or an interface. These sealed classes and interfaces are implemented using sealed, non-sealed and permits keywords. These are the new keywords in Java which are introduced from java 17. These sealed classes and interfaces are introduced in java 15 itself but as a preview feature. From Java 17, they have made permanent. Let’s see Sealed classes and interfaces in Java 17 in detail.
What is Sealed Class ?
Inheritance most important concept in object oriented programming and normally what we do is, we will create a class and then some other class will inherit it with the help of extends keyword in java. Basically the thing is if you want to restrict the inheritance. Suppose I have a class I don't want this to be extended by some other class then you can make the class final. So you can restrict it or you can allow any class to extend it without making it final. But what if you want to do a partial restriction, where you say, I have a lass A and I want class B and C to extend it but no other class in the world should be able to extend my class. How will you restrict that because with the help of final keyword you are restricting to all the classes even B and C. But now we want to allow it and that's where you can use something called sealed class. Now using a sealed class what you can do is you can permit the number of Classes.
Example, you can say, I'm permitting class B and class C but no other class in the world and that's how you can restrict but partially in java with the help of sealed classes.
Example, you can say, I'm permitting class B and class C but no other class in the world and that's how you can restrict but partially in java with the help of sealed classes.
Advantages of Sealed Class and Interface
- It allows permission to the sub/child classes that can extend the sealed super/parent class.
- It makes super/parent class broadly accessible but not broadly extensible.
- It allows compilers to enforce the type system on the users of the class.
- Developer of a super/parent class gets control over the sub/child classes. Hence, they can define methods in a more restricted way.
Defining a Sealed Class
The declaration of a sealed class is not much complicated (relatively straightforward). If we want to declare a class as sealed, add a sealed modifier to its declaration. After the class declaration and extends and implements clause, add permits clause. The clause denotes the classes that may extend the sealed class.It presents the following modifiers and clauses:
sealed: It can only be extended by its permitted subclasses.
non-sealed: It can be extended by unknown subclasses; a sealed class cannot prevent its permitted subclasses from doing this.
permits: It allows the subclass to inherit and extend.
final: The permitted subclass must be final because it prevents further extensions.
Sealed classes and Interfaces in Java 17 (Short description)
b) Sealed classes are declared using sealed modifier and permits clause. Permits clause specifies the sub classes which can extend the current sealed classes.
c) Permitted sub classes must be in the same package or in the same module as that of a sealed super class.
d) Permitted subclasses must be either final or sealed or non-sealed. If you don’t declare permitted sub classes with any one of these modifiers, you will get compile time error.
e) Final permitted subclasses cannot be extended further, sealed permitted subclasses are extended by only permitted subclasses and non-sealed permitted subclasses can be extended by anyone.
f) Sealed class must and should specify its permitted subclasses using permits clause otherwise there will be compilation error.
g) Permitted sub interfaces must be either sealed or non-sealed but not final.
h) Permitted sub types must have name. Hence, anonymous inner classes or local inner classes cannot be permitted sub types.
i) A sealed super class can be abstract, and permitted sub classes can also be abstract provided they can be either sealed or non-sealed but not final.
j) While declaring sealed classes and sealed interfaces, permits clause must be used after extends and implements clause.
Sealed Classes And Interfaces in Java17
Sealed classes and interfaces restrict which classes or interfaces can extend or implement them. It gives control to the owner of a class or an interface to declare which classes or interfaces can extend or implement or modify or reuse their work. Sealed classes and interfaces can be extended or implemented by only permitted classes and interfaces. Let’s see them one by one in detail.1) Sealed classes are declared using sealed modifier and permits clause. permits clause specifies the sub classes which can extend the current sealed class.
package Java17Features;
sealed class ParentClass permits ChildClassOne, ChildClassTwo{
// Sealed Parent Class
}
final class ChildClassOne extends ParentClass{
// Final Child Class One
}
final class ChildClassTwo extends ParentClass {
// Final Child Class Two
}
2) Permitted sub classes must be in the same package or in the same module as that of a sealed super class.
3) Permitted sub classes must be either final or sealed or non-sealed. If you don’t declare permitted sub classes with any one of these modifiers, you will get compile time error.
package Java17Features;
sealed class ParentClass permits ChildClassOne, ChildClassTwo, ChildClassThree{
// Sealed Parent Class
}
final class ChildClassOne extends ParentClass{
// Final Child Class One
}
sealed class ChildClassTwo extends ParentClass permits AnotherChildClass {
// Sealed subclass permitting another child class to extend it further
}
non-sealed class ChildClassThree extends ParentClass{
// non-sealed child class
}
final class AnotherChildClass extends ChildClassTwo {
// Final child class of child class two
}
5) Sealed class must and should specify its permitted sub classes using permits clause otherwise there will be a compilation error.
sealed class SomeClass
{
//Compile Time Error : Sealed class must specify permitted subclasses
}
package Java17Features;
sealed class ParentClass permits ChildClassOne, ChildClassTwo{
// Sealed Parent Class
}
final class ChildClassOne {
// Compile time error : It must extend ParentClass
}
non-sealed class ChildClassTwo {
// Compile time error : It must extend ParentClass
}
package Java17Features;
sealed interface ParentInterface permits ChildClass, ChildInterface{
// Sealed Parent Interface
}
non-sealed interface ChildInterface extends ParentInterface {
// Non-sealed child interface
}
non-sealed class ChildClass implements ParentInterface {
// Non-sealed child class
}
package Java17Features;
sealed interface ParentInterface permits ChildInterfaceOne, ChildInterfaceTwo, ChildInterfaceThree{
// Sealed Parent Interface
}
sealed interface ChildInterfaceOne extends ParentInterface permits ChildClass {
// sealed child interface
}
non-sealed class ChildClass implements ChildInterfaceOne {
// Non-sealed child class implementing ChildInterfaceOne
}
non-sealed interface ChildInterfaceTwo extends ParentInterface {
// Non-sealed Child Interface
}
final interface ChildInterfaceThree extends ParentInterface {
// Compile time error : Permitted child interface must not be final
}
10) A sealed super class can be abstract, and permitted sub classes can also be abstract provided they can be either sealed or non-sealed but not final.
package Java17Features;11) While declaring sealed classes and sealed interfaces, permits clause must be used after extends and implements clause.
abstract sealed class ParentClass permits ChildClassOne, ChildClassTwo, ChildClassThree
{
//Parent class can be abstract and Sealed
}
abstract final class ChildClassOne extends ParentClass
{
//Compile Time Error : Child class can't be final and abstract
}
abstract non-sealed class ChildClassTwo extends ParentClass
{
//Child class can be abstract and Non-sealed
}
abstract sealed class ChildClassThree extends ParentClass permits SomeChildClass
{
//Child class can be abstract and Sealed
}
final class SomeChildClass extends ChildClassThree
{
//Final Child class of ChildClassThree
}
12) With the introduction of sealed classes, two more methods are added to java.lang.Class (Reflection API). They are getPermittedSubclasses() and isSealed().
0 Comments