Sealed Class in Java Last Updated : 28 Apr, 2025 Comments Improve Suggest changes Like Article Like Report In Java, we have the concept of abstract classes. It is mandatory to inherit from these classes since objects of these classes cannot be instantiated. On the other hand, there is a concept of a final class in Java, which cannot be inherited or extended by any other class. What if we want to restrict the number of classes that can inherit from a particular class? The answer is sealed class. So, a sealed class is a technique that limits the number of classes that can inherit the given class. This means that only the classes designated by the programmer can inherit from that particular class, thereby restricting access to it. when a class is declared sealed, the programmer must specify the list of classes that can inherit it. The concept of sealed classes in Java was introduced in Java 15. Steps to Create a Sealed ClassDefine the class that you want to make a seal.Add the "sealed" keyword to the class and specify which classes are permitted to inherit it by using the "permits" keyword.Examplesealed class Human permits Manish, Vartika, Anjali{ public void printName() { System.out.println("Default"); }}non-sealed class Manish extends Human{ public void printName() { System.out.println("Manish Sharma"); }}sealed class Vartika extends Human{ public void printName() { System.out.println("Vartika Dadheech"); }}final class Anjali extends Human{ public void printName() { System.out.println("Anjali Sharma"); }}Explanation of the above Example:Human is the parent class of Manish, Vartika, and Anjali. It is a sealed class so; other classes cannot inherit it.Manish, Vartika, and Anjali are child classes of the Human class, and it is necessary to make them either sealed, non-sealed, or final. Child classes of a sealed class must be sealed, non-sealed, or final.If any class other than Manish, Vartika, and Anjali tries to inherit from the Human class, it will cause a compiler error.Code Implementation Java import java.lang.*; sealed class Human permits Manish, Vartika, Anjali { public void printName() { System.out.println("Default"); } } non-sealed class Manish extends Human { public void printName() { System.out.println("Manish Sharma"); } } sealed class Vartika extends Human { public void printName() { System.out.println("Vartika Dadheech"); } } final class Anjali extends Human { public void printName() { System.out.println("Anjali Sharma"); } } public class Main { public static void main(String[] args) { Human h1 = new Anjali(); Human h2 = new Vartika(); Human h3 = new Manish(); h1.printName(); h2.printName(); h3.printName(); } } Output:Â Comment More infoAdvertise with us Next Article Sealed Class in Java elite_programmer Follow Improve Article Tags : Java Practice Tags : Java Similar Reads Object Class in Java Object class in Java is present in java.lang package. Every class in Java is directly or indirectly derived from the Object class. If a class does not extend any other class then it is a direct child class of the Java Object class and if it extends another class then it is indirectly derived. The Ob 7 min read Types of Classes in Java A class is a blueprint in the Java programming language from which an individual object can be built. In Java, we may declare a class by using the class keyword. Class members and functions are declared simply within the class. Classes are required for the creation of Java programs. The object-orien 8 min read Java.lang.Runtime class in Java In Java, the Runtime class is used to interact with Every Java application that has a single instance of class Runtime that allows the application to interface with the environment in which the application is running. The current runtime can be obtained from the getRuntime() method. Methods of Java 6 min read Classes and Objects in Java In Java, classes and objects are basic concepts of Object Oriented Programming (OOPs) that are used to represent real-world concepts and entities. The class represents a group of objects having similar properties and behavior, or in other words, we can say that a class is a blueprint for objects, wh 11 min read Java.util.Random class in Java Random class is used to generate pseudo-random numbers in java. An instance of this class is thread-safe. The instance of this class is however cryptographically insecure. This class provides various method calls to generate different random data types such as float, double, int. Constructors: Rando 4 min read Like