Sealed Class in Java
In programming, security and control flow are the two major concerns that must be considered while developing an application. There are various controlling features such as the use of final and protected keyword restricts the user to access variables and methods. Java 15 introduces a new preview feature that allows us to control the inheritance. In this section, we will discuss the preview feature, the concept of sealed class, and interface with proper examples.
Java Sealed Class
Java 15 introduced the concept of sealed classes. It is a preview feature. Java sealed classes and interfaces restrict that which classes and interfaces may extend or implement them.
In other words, we can say that the class that cannot be inherited but can be instantiated is known as the sealed class. It allows classes and interfaces to have more control over their permitted subtypes. It is useful both for general domain modeling and for building a more secure platform for libraries.
Note that the concept of sealed classes is a preview feature, not a permanent feature.
A feature whose design, implementation, and specification are complete but not permanent. In future Java SE releases, the feature may exist in different forms or not at all.
Also, note that the code that has preview feature cannot be compile and run easily. It requires additional command-line options.
Using Preview Features
If we are using the preview feature in a Java program, we must explicitly enable the preview feature in the compiler and runtime systems. If we do not enable the preview feature, we get an error message "preview feature is disabled by default".
Use the following command to compile a Java program that has the preview feature:
Where n is the JDK version.
Suppose, Demo.java is a source file that has a preview feature and we want to compile and run it.
When we compile the above source file, we get the following warning message on the console.
Note: Demo.java uses preview language features.
Note: Recompile with -Xlint:preview for details
Use the following command to run a Java program that has the preview feature:
Note: It is not necessary that the code using a preview feature of an older version of Java SE compile or run on a newer release.
Uses of Sealed Class
Sealed classes work well with the following:
Advantages of Sealed Class and Interface
Defining a Sealed Class
The declaration of a sealed class is not much complicated. 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:
For example, the following declaration of Subjects class specifies four permitted subclasses, English, Science, Mathematics, and Physics.
Let's define the four permitted subclasses, English, Science, Mathematics, and Physics, in the same module or package in which the sealed class is defined.
The Mathematics class has a further subclass, AppliedMathematics:
On the other hand, we can also define permitted subclasses in the same file as the sealed class. In such a case, we can omit the permits clause:
Constraints on Permitted Subclass
If a class is defined as sealed, it enforces the following three limitations on its permitted subclass.
1. All the permitted subclasses must belong to the same module or package as the sealed class. For example:
Suppose, we have the same unnamed module and the following packages:
We get an error "The class is not allowed to extend the sealed class from another package or module."
2. Every permitted subclass must explicitly extend the sealed class.
3. Every permitted subclass must define a modifier: final, sealed, or non-sealed.
Like the sealed classes, we can also define a sealed interface just by adding a sealed modifier before the interface name. After that use extends keyword (if require), and then define permits clause.
The interface only permits Mango and Pineapple to implement it.
Let's understand the concept of Sealed class through a Java program.
Note: Before exacting the following program, ensure that Java 15 or a later version is installed in your system. Otherwise, you will get errors.
Example of Sealed Class
The age of grandfather is: 87