parent package class accessible from child packge class in java?

后端 未结 6 1142
予麋鹿
予麋鹿 2021-01-02 06:56

In java parent package class accessible from child packge class? please explain me any one?

example package A.A1.A2 contains class sub package A contains class sup

6条回答
  •  谎友^
    谎友^ (楼主)
    2021-01-02 07:35

    Java does not recognize the notion of a subpackage1. As far as Java is concerned packages a and a.b and a.b.c are unrelated. They are just names.

    So, if you want to access a.b.SomeClass from a.b.c.SomeOtherClass, you must either use a fully qualified class name, or add an import to SomeOtherClass

    1 - From Java 9 onwards you can use modules to implement abstraction boundaries that are larger than a single package. This doesn't address this question which is about package-private access, but it could be viewed as an alternative to package-private.


    As for your example that doesn't compile, I think we need a proper MCVE to understand that. My guess is that you have gotten the file organization for your source tree wrong ...

    It is also possible that the problem is that the visibility of the class you are trying to import is wrong (package private), but that wouldn't cause the compiler to say that the package doesn't exist, like you said it does.

提交回复
热议问题