Sealed Class in JavaIn 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 ClassJava 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. Preview FeatureA 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 FeaturesIf 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: or 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 detailsUse 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 ClassSealed classes work well with the following:
Advantages of Sealed Class and Interface
Defining a Sealed ClassThe 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. Subjects.java 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. English.java Science.java Mathematics.java The Mathematics class has a further subclass, AppliedMathematics: AppliedMathematics.java Physics.java 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 SubclassIf 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: or 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. Sealed InterfaceLike 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 ClassSealedClassExample.java Output: The age of grandfather is: 87 Next TopicCamel case in Java |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India