'What are sealed classes in Java 17?
Today, I updated my Java version from 16 to 17, and I found that sealed
classes is a new feature in it. I think it can be declared like this:
public sealed class Main permits AClass, AnotherClass {
}
But, what is the use of sealed classes in Java?
I also knew that it was a preview feature in jdk-15.
Solution 1:[1]
You can follow this link for examples.
In short sealed classes gives you the control of which models, classes etc. that can implement or extend that class/interface.
Example from the link:
public sealed interface Service permits Car, Truck {
int getMaxServiceIntervalInMonths();
default int getMaxDistanceBetweenServicesInKilometers() {
return 100000;
}
}
This interface only permits Car and Truck to implement it.
Solution 2:[2]
The JEP 409 explains it as
A sealed class or interface can be extended or implemented only by those classes and interfaces permitted to do so.
A more practical explanation is the following:
The situation in the past was:
- You could not restrict an interface being extended by another interface
- You could not constraint which classes where able to implement a specific interface.
- You had to declare a class as final in order to not be extended by another class. This way no class could extend the declared final class. This was black or white approach.
The current situation with sealed keyword is:
You can now restrict an interface being extended by other interfaces and make a rule for only some specific interfaces which will be allowed to extend it.
Example:
public sealed interface MotherInterface permits ChildInterfacePermitted {} //Has to be declared either as sealed or non-sealed public non-sealed interface ChildInterfacePermitted extends MotherInterface {} public interface AnotherChildInterface extends MotherInterface {} //compiler error! It is not included in the permits of mother inteface
You can now create an interface and select only specific classes that are allowed to implement that interface. All other classes are not allowed to implement it.
Example:
public sealed interface MotherInterface permits ImplementationClass1 {} //Has to be declared either as final or as sealed or as non-sealed public final class ImplementationClass1 implements MotherInterface {} public class ImplementationClass2 implements MotherInterface {} //compiler error! It is not included in the permits of mother inteface
You can now restrict a class being extended (same as before with final) but you can now allow some specific classes to extend it. So now you have more control as before the keyword final was absolute restricting every class from extending the declared final class
Example:
public sealed class MotherClass permits ChildClass1 {} //Has to be declared either as final or as sealed or as non-sealed public non-sealed class ChildClass1 extends MotherClass {} public class ChildClass2 extends MotherClass {} //compiler error! It is not included in the permits of MotherClass
Important notes:
The sealed class and its permitted subclasses must belong to the same module, and, if declared in an unnamed module, to the same package.
Example:
Let's say that we have the same unnamed module and the following packages
-packageA -Implementationclass1.java -packageB -MotherClass.java
or
-root -MotherClass.java -packageA -Implementationclass1.java
You will get the error Class is not allowed to extend sealed class from another package. So if you have an unnamed module all participating classes and interfaces for the sealed function must be placed exactly on the same package.
Every permitted subclass must directly extend the sealed class.
Solution 3:[3]
Sealed Classes
- A sealed class is a class or interface which restricts which other classes or interfaces may extend it.
- Sealed classes and interfaces represent restricted class hierarchies that provide more control over an inheritance.
- All direct subclasses of a sealed class are known at compile time. No other subclasses may appear after a module with the sealed class is compiled.
- For example, third-party clients can't extend your sealed class in their code. Thus, each instance of a sealed class has a type from a limited set that is known when this class is compiled.
Defining Sealed Classes
To seal a class, add the sealed modifier to its declaration. Then, after any extends and implements clauses, add the permits clause. This clause specifies the classes that may extend the sealed class.
For example, the following declaration of Shape specifies three permitted subclasses, Circle, Square, and Rectangle:
public sealed class Shape
permits Circle, Square, Rectangle {
}
Define the following three permitted subclasses, Circle, Square, and Rectangle, in the same module or in the same package as the sealed class:
public final class Circle extends Shape {
public float radius;
}
public non-sealed class Square extends Shape {
public double side;
}
public sealed class Rectangle extends Shape permits FilledRectangle {
public double length, width;
}
Rectangle has a further subclass, FilledRectangle:
public final class FilledRectangle extends Rectangle {
public int red, green, blue;
}
Constraints on Permitted Subclasses
They must be accessible by the sealed class at compile time.
For example, to compile
Shape.java
, the compiler must be able to access all of the permitted classes of Shape:Circle.java
,Square.java
, andRectangle.java
. In addition, because Rectangle is a sealed class, the compiler also needs access toFilledRectangle.java
.They must directly extend the sealed class.
They must have exactly one of the following modifiers to describe how it continues the sealing initiated by its superclass:
- final: Cannot be extended further
- sealed: Can only be extended by its permitted subclasses
- non-sealed: Can be extended by unknown subclasses; a sealed class cannot prevent its permitted subclasses from doing this
For example, the permitted subclasses of Shape demonstrate each of these three modifiers:
Circle
is final whileRectangle
is sealed and TheSquare
is non-sealed.They must be in the same module as the sealed class (if the sealed class is in a named module) or in the same package (if the sealed class is in the unnamed module, as in the Shape.java example).
For example, in the following declaration of com.example.graphics.Shape
, its permitted subclasses
are all in different packages. This example will compile only if Shape
and all of its permitted subclasses are in the same-named module.
package com.example.graphics;
public sealed class Shape
permits com.example.polar.Circle,
com.example.quad.Rectangle,
com.example.quad.simple.Square { }
Solution 4:[4]
As per this documentation, Sealed classes and interfaces restrict which other classes or interfaces may extend or implement them. It is more of a declarative way to restrict the use of a superclass rather than using access modifiers.
In Java, a class can be final so no other classes can subclass it. If a class is not final, then it is open to all other classes to support code reusability. Doing so would raise data modeling concerns.
The below NumberSystem class is open to all classes, so any subclass can extend it. What if you want to restrict this NumberSystem to a fixed set of subclasses (Binary, Decimal, Octal, and HexaDecimal)?. It means you don’t want any other arbitrary class to extend this NumberSystem class.
class NumberSystem { ... }
final class Binary extends NumberSystem { ... }
final class Decimal extends NumberSystem { ... }
final class Octal extends NumberSystem { ... }
final class HexaDecimal extends NumberSystem { ... }
Using sealed class, you can achieve it by controlling the subclasses that can extend it and prevent any other arbitrary class from doing so.
Solution 5:[5]
All sealed
java classes or interfaces must use permits
keyword. For example:
Parent.class:
public sealed class Parent permits Child1, Child2 {
void parentMethod() {
System.out.println("from a sealed parent class ");
}
}
Child1.java:
public final class Child1 extends Parent {
public static void main(String[] args) {
Child1 obj = new Child1();
obj.parentMethod();
}
}
Child2.java:
public final class Child2 extends Parent {
public static void main(String[] args) {
Child2 obj = new Child2();
obj.parentMethod();
}
}
Child3.java
public final class Child3 extends Parent {
public static void main(String[] args) {
Child3 obj = new Child3();
obj.parentMethod();
}
}
This Child3
class code will throw a compile-time error saying
The type Child3 extending a sealed class Parent should be a permitted subtype of Parent (permits Child3
, just like Child1
and Child2
).
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | svick |
Solution 2 | |
Solution 3 | |
Solution 4 | Praj |
Solution 5 | Christopher |