简体   繁体   中英

Making package-protected java class visible in parent package

I have two packages

uk.co.planetbeyond.data

and

uk.co.planetbeyond.data.bean

How can I make the classes present in uk.co.planetbeyond.data.bean visible to classes in parent package uk.co.planetbeyond.data but invisible in other packages?

Is it even possible?

As far as I'm aware, it's not possible. While we tend to think of packages as forming a hierarchy, they don't as far as the Java language is concerned.

As stated in all the other answers: No. The package hierarchy means nothing outside of the mind of the person who set it up.

In my opinion this is Java's major flaw. When building up monster systems by merging together other monster programs, nothing beats Java-the-language. All other languages eventually end up in confused, undocumentable, indecipherable piles of code.

Well, so does Java, but it gets a lot further than any other language I've used. But its ignorance of package hierarchy--the inability to encapsulate data in a package hierarchy--is the limitation, or brick wall, that stops it. I can put a system, or set of classes that work together, in a single package. Then I can make a bunch of those classes package private so that when this package is added to another super-system that super-system cannot see--and be confused by--those hidden classes.

But if my system is already super enough ("super" as in "superman", not "superclass") to need several packages for clarity, my formerly package-private classes must now be public, and I can't stop the super-super-system from seeing everything in my super-system.

To make a vast, complex system work, the complexity at every point must be minimized. My super-system has to look as simple as possible to the super-super-system I'm adding it to. Having all the pipes and wires and beams and welds sitting out in public display because I cannot make them package-level-private does not help this.

I hope Jigsaw, mentioned elsewhere, will help. I also hope developer tools such as IDEs will start displaying packages in an outline format rather than treating ab and abcde as if they were all on the same level--just elements in the same list.

Currently, this is not possible. Java packages are not really hierarchical in nature - classes are either in the same package or they are not.

However, this is likely one of the things that will be addressed by Project Jigsaw , which may be included in Java 8. That should allow creating classes which are public within all the packages of a module, but are not exported to other modules.

No, it is not possible. Package visibility cannot selectively extend to other packages. A class may only be private (that is, visible only to a non-private enclosing class), package-private (no modifier, visible to only to other classes in the same package), or public (visible to all classes).

As far as i know it is not possible in Java. For more information see the link here which explains the access modifiers in Java clearly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM