Sealed Class in Java10 Sept 2024 | 6 min read 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 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 |
Java Hexadecimal to Binary Conversion
In programming, converting one type to/ from a type is a crucial task. Sometimes we require, conversion from one type to another type. In the Java conversion section, we have discoed various types of conversions. In this section, we can discuss how to convert hexadecimal into...
7 min read
Spirally Traversing a Matrix in Java
Traversing a matrix spirally entails moving through the elements in a circular pattern, starting from the top-left corner and moving to the right along the top row. Following every row or column traversal, the boundaries are adjusted, and the direction is switched, persisting until all elements...
10 min read
Special Number in Java
In this section, we will learn what is a special number and also create Java programs to check if the given number is a special number or not. The special number program frequently asked in Java coding tests and academics. Special Number If the sum of the factorial...
3 min read
Advantages of Abstraction in Java
Abstraction is a fundamental concept in object-oriented programming (OOP), and it plays a crucial role in making Java a powerful and flexible programming language. Abstraction allows developers to create complex systems while managing the complexity through encapsulation and simplification of details. In Java, abstraction is...
9 min read
Dynamic Initialization in Java
Java is a versatile and powerful programming language known for its robustness and flexibility. One of the many features that make Java a popular choice among developers is dynamic initialization. Dynamic initialization allows you to initialize variables and objects at runtime, providing you with greater...
9 min read
Java Output Formatting
At times we want the output of a program to be printed in a given specific format. In the C programming language this is possible using the printf( ) function. In this section, we will discuss the different output formatting. Let's discuss how we can format the...
4 min read
Lazy Loading in Java
The concept of delaying the loading of an object until one needs it is known as lazy loading. In other words, it is the process of delaying the process of instantiating the class until required. Lazy loading is important in a scenario where the cost of...
4 min read
LCA in Binary Tree in Java
The goal is to determine the lowest common ancestor of the two nodes given the root of a Binary Tree with all unique values and two node values n1 and n2. The LCA of n1 and n2 is the shared ancestor of n1 and n2...
16 min read
ID Card Generator Using Java
This article will guide you through the steps involved in utilizing the Java programming language to create an interactive ID card generator. This study aims to provide an interesting practical experience while demystifying the complexities of Java's core ideas. It begins with a simple console-based application...
6 min read
Overlapping Intervals Problem in Java
The overlapping intervals problem serves as an essential computational challenge that applies to scheduling applications while it also functions in computational geometry and range merging tasks. When provided with a range of intervals, the objective is to process them swiftly for combined interval detection. Two intervals [a,...
5 min read
We request you to subscribe our newsletter for upcoming updates.

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