Junior — Middle
Is it possible to create inheritance from a class declared as Sealed in Java?
sobes.tech AI
Answer from AI
In Java, a class declared as sealed restricts the set of classes that can inherit from it. Inheritance from a sealed class is possible only if the subclass is explicitly listed in the permits clause of the sealed class declaration.
Example:
public sealed class Shape permits Circle, Rectangle {
//...
}
public final class Circle extends Shape {
//...
}
public final class Rectangle extends Shape {
//...
}
Attempting to create a class not listed in permits will result in a compile-time error. Thus, inheritance from a sealed class is possible but strictly controlled.