sealed classes and interfaces can't permit classes or records from sub packages
Tagir Valeev
amaembo at gmail.com
Sat Dec 28 06:23:56 UTC 2024
Hello!
There's no such thing as a 'subpackage' in Java. Two packages having the
same name prefix are just two different packages. If you want to group
several packages together, a proper mechanism for this in Java is declaring
a JPMS module. If you declare a module, you'll be able to put the
implementations of your sealed interface into different packages within the
module.
With best regards,
Tagir Valeev
On Sat, Dec 28, 2024, 05:15 david Grajales <david.1993grajales at gmail.com>
wrote:
> Hello Amber Team,
>
> I’ve observed a counter intuitive behavior, and I’m unsure whether it’s
> intentional or a potential issue.
>
> Here’s the scenario:
>
> 1. I have a package named vehicles containing a *sealed interface*
> called Vehicle.
> 2. Inside this vehicles package, I’ve created two sub-packages:
> - land-vehicles
> - water-vehicles
> 3. Each sub-package contains two records implementing the Vehicle
> interface.
>
> However, the compiler raises an error when I attempt this setup:
>
> "A class is not allowed to extend or implement a sealed class from another
> package."
>
> This seems counterintuitive because all components are conceptually part
> of the same package with the records merely placed in sub-packages for
> better organization.
>
> Here’s a textual representation of the package layout for reference:
>
>
> vehicles/
>
> ├── Vehicle.java // Sealed interface
>
> ├── land-vehicles/
>
> │ ├── Car.java // Record implementing Vehicle
>
> │ ├── Motorcycle.java // Record implementing Vehicle
>
> ├── water-vehicles/
>
> ├── Boat.java // Record implementing Vehicle
>
> ├── Submarine.java // Record implementing Vehicle
>
>
> Could you clarify whether this behavior is intended or a bug? If not,
> could this be an area for potential improvement?
>
> I am using openjdk 23.0.1
>
> Thank you for your time and assistance!
>
> Best regards and I hope you all have a nice new year.
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/amber-dev/attachments/20241228/6782d812/attachment.htm>
More information about the amber-dev
mailing list