Sealed classe/Interface
Sealed class/Interface enables more fine-grained inheritance control in
Sealing a class/Interface means we are defining the permitted subtypes.
In simple words we can say that sealing a class or interface we are going to instruct
those who will be able to extend it.somehow it’s providing extra security to classes and interfaces.
Only permitted subtypes will be able to extend sealed classes/interfaces.
In java if you have a final class , it can not be extended, if it’s abstract anyone can.
extend it BUT we had no control on who can extend the classes.in simple words we
can say that class can either be extended or cant be BUT we can’t restrict this extension to a given class.this is what sealed classes will do.
Sealed classes/Interface can only be accessible for those whom you will permit, anyone else will NOT be able to extend it.
Sealed Interface:
package palywithjava.in.sealedclasses;
public sealed interface OrgService permits
EmployeeService,TraineeService,SealedTester {
int getAge();
int getSalary();
}From interface declaration, it’s clear that Only EmployeeService(I),TraineeService(I) and SealedTester(C) will be able to extend the sealed Interface OrgService
Main purpose of using sealed classes/Interfaces is to provide modular security to
our classes so that nobody will be able to access them until we are permitting them.
We need to follow some guidelines while using sealed classes.
- Sealed classes/Interfaces can be extended within the package.
- Sub classes/interfaces should implement/extend the sealed class/Interface.
- Sub classes/interfaces should have either of the access modifier final,sealed and non-sealed.
final : it can’t be extended.
Get Playwithjava’s stories in your inbox
Join Medium for free to get updates from this writer.
sealed: only permitted type can extend.
non-sealed: this type will be available to be extended/implemented by any class/interface.
Code walkthrough:
package palywithjava.in.sealedDemo;
public sealed interface OrgService permits EmployeeService,TraineeService {
int getAge();
int getSalary();
}If the subtype is a non-sealed interface then it will be extensible by everyone.
package palywithjava.in.sealedDemo;
public non-sealed interface EmployeeService extends
OrgService {
String getEmployeeName();
}If the subtype is a sealed interface then it will NOT be extensible by everyone.
package palywithjava.in.sealedDemo;
public class EmployeeImpl implements EmployeeService{
@Override
public String getEmployeeName() {
return "Amit";
}
@Override
public int getAge() {
return 28;
}
@Override
public int getSalary() {
return 331110;
}
}package palywithjava.in.sealedDemo;
public sealed interface TraineeService extends OrgService
permits SealedTesterIntrface{
String getTraineeName();
}package palywithjava.in.sealedDemo;
public non-sealed interface SealedTesterIntrface extends
TraineeService {
}package palywithjava.in.sealedDemo;
public class TraineeImpl implements SealedTesterIntrface{
@Override
public int getAge() {
return 25;
}
@Override
public int getSalary() {
return 333330;
}
@Override
public String getTraineeName() {
return "Raj";
}
}Let’s try to extend this interface from other Interface which is there in other package.
You can see the error “Class is not allowed to extend sealed class from another package”
You can use collections to get some info about Sealed Classes:
sealed class Product permits Laptop {
}
non-sealed class Laptop extends Product{
}public class DemoTest {
public static void main(String[] args) {
Product p=new Product();
Product c = new Product();
Class cz = c.getClass();
Class<?>[] permittedSubclasses = cz.getPermittedSubclasses();
for (Class<?> sc : permittedSubclasses){
System.out.println(cz.isSealed()+" "+sc.getName());
}
}
}Result: true Laptop





