If I declare following sealed hierarchy
package a;
import b.B;
public sealed interface A permits B {
}
package b;
import a.A;
public record B() implements A {
}
without using modules (no module-info.java) and try to compile it with Maven I get
[ERROR] .../src/main/java/a/A.java:[5,35] class a.A in unnamed module cannot extend a sealed class in a different package
I'm aware of https://openjdk.java.net/jeps/409 and this section:
The classes specified by permits must be located near the superclass: either in the same module (if the superclass is in a named module) or in the same package (if the superclass is in the unnamed module).
However, shouldn't Maven by default use classpath while compiling? Can this limitation be avoided at all?
If not, doesn't this set a precedent where a feature on module path is more flexible than on class path and that in turn - while classpath is still supported, it's not as first class citizen as it used to be when compared to module path?
Sealed classes and interfaces represent restricted class hierarchies that provide more control over inheritance. All direct subclasses of a sealed class are known at compile time. No other subclasses may appear outside a module within which the sealed class is defined.
This means the heir of the sealed class can have as many as any number of instances and can store states, but the enum class cannot. Sealed classes also offer a restricted number of hierarchies. This means if you have a different class defined in another file in your project, you cannot extend the class Menu .
A sealed class, in C#, is a class that cannot be inherited by any class but can be instantiated. The design intent of a sealed class is to indicate that the class is specialized and there is no need to extend it to provide any additional functionality through inheritance to override its behavior.
Sealed class is used to stop a class to be inherited. You cannot derive or extend any class from it. Sealed method is implemented so that no other class can overthrow it and implement its own method.
A sealed class is abstract by itself, it cannot be instantiated directly and can have abstract members. Constructors of sealed classes can have one of two visibilities: protected (by default) or private: Direct subclasses of sealed classes and interfaces must be declared in the same package.
All direct subclasses of a sealed class are known at compile time. No other subclasses may appear outside a module within which the sealed class is defined. 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.
Sealed classes and interfaces restrict which other classes or interfaces may extend or implement them. For background information about sealed classes and interfaces, see JEP 409 .
Constructors of sealed classes can have one of two visibilities: protected (by default) or private: Direct subclasses of sealed classes and interfaces must be declared in the same package. They may be top-level or nested inside any number of other named classes, named interfaces, or named objects.
The classpath is the unnamed module.
The motivation is that a sealed class and its (direct) subclasses are tightly coupled, since they must be compiled and maintained together. In a modular world, this means "same module"; in a non-modular world, the best approximation for this is "same package".
So yes, if you use modules, you get some additional flexibility, because of the safety boundaries modules give you.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With