Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making package-protected java class visible in parent package

Tags:

java

People also ask

Is protected visible to classes within the package?

The protected access modifier is accessible within package and outside the package but through inheritance only. The protected access modifier can be applied on the data member, method and constructor. It can't be applied on the class. It provides more accessibility than the default modifer.

Is private method accessible from inside all classes in the same package?

Public methods inside a package class are public to classes in the same package. But, private methods will not be accessible by classes in the same package.

How can you access a class declared as private outside its packages?

Can a class declared as private be accessed outside it's package? Not possible. Can a class be declared as protected? The protected access modifier cannot be applied to class and interfaces.

Can I use protected class in Java?

Protected Access Modifier - Protected Variables, methods, and constructors, which are declared protected in a superclass can be accessed only by the subclasses in other package or any class within the package of the protected members' class. The protected access modifier cannot be applied to class and interfaces.


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 a.b and a.b.c.d.e as if they were all on the same level--just elements in the same list.


Short answer, it's not possible unless for subclasses of classes in your uk.co.planetbeyond.data.bean package.

You may be interested in reading:

http://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html

Isn't "package private" member access synonymous with the default (no-modifier) access?

In Java, difference between default, public, protected, and private

Java - Method accessibility inside package-private class?


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.