0

Java doesn't have the concept of Subpackage visibility

I know that :(
Java: Subpackage visibility?

so if I have a Class A (package visible) inside the package com.example.foo
and another Class B (package visible) inside the package com.example.foo.bar

+-- org.example
    |
    +-- foo
        |
        +-- A
        |
        +-- bar
            |
            +-- B

they are like actually in different packages and then one class is not visible from the other.

One thing that I always like to know is the WHY?

The structure folder of the package would encourage a mechanism like subpackage visibility.
So that the Class with higher hierarchy can access to the one with lower hierarchy.

In this example:
Class A should be able to access to Class B
(Similarly to that happen to the inner classes)

If that mechanism would be in place we could organize better our code and have better incapsulation.
How to organize code logically into package while preserving encapsulation

This would be a very nice feature for the Java world.

Without wanting to criticize, why has been taken that kind of decision for the package visible?
And why the Subpackage visibility is not taking into consideration?

gixlg
  • 1,193
  • 1
  • 9
  • 21
  • 3
    My guess is because there really is not concept of a subpackage in java. – NomadMaker Jul 09 '20 at 21:32
  • Your question is already answered by the first question you link: Java doesn't have sub-packages, so therefor it cannot have sub-package visibility. That packages may seem hierarchical because of how they are commonly stored in the filesystem, does not mean they are actually hierarchical. – Mark Rotteveel Jul 11 '20 at 11:42

1 Answers1

0

You're asking for the reasons that a decision made 25 years ago was made?

I... don't think the handful of folks who made that call hang out on stackoverflow.

The spec says that there's no such thing as a subpackage. package com.foo.bar has zero relation to package com.foo.

The jigsaw system fixes the primary issue, so the problem has been addressed.

rzwitserloot
  • 85,357
  • 5
  • 51
  • 72
  • Not strictly true. Java has subpackages per the JLS, but they do nothing other than add some naming constraints preventing classes from sharing the same name as the subpackage. In particular, as you mention, they don't confer any special access relation between them. — https://stackoverflow.com/a/59956720/1108305 – M. Justin Dec 23 '20 at 07:13