Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sealed classes for classes in different packages

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?

like image 472
Lovro Pandžić Avatar asked Jul 23 '21 08:07

Lovro Pandžić


People also ask

What are sealed classes?

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.

Can sealed class extend another class?

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 .

What is sealed class with example?

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.

Where can you use sealed classes?

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.

What is a sealed class in Java?

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.

When are the subclasses of a sealed class known?

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.

What are sealed classes and interfaces?

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 .

Can a sealed class have a private constructor?

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.


1 Answers

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.

like image 73
Brian Goetz Avatar answered Oct 16 '22 17:10

Brian Goetz