Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

accessing protected properties from java subpackages

Tags:

java

package

suppose I have package J

moreover I create a new folder in J hence it becomes a subpackage of J, let's say it's J.E

suppose I have a class in J named H with protected properties, and another class named T in J.E

can class T access the protected properties of H?

like image 716
kamikaze_pilot Avatar asked Dec 30 '25 02:12

kamikaze_pilot


1 Answers

Sub-packages are useful only as an organizational concept. They can never be used for access control; no access relationships exist between a parent package and a child package.

From the Java Language Specification:

7.1 Package Members

...

The hierarchical naming structure for packages is intended to be convenient for organizing related packages in a conventional manner, but has no significance in itself other than the prohibition against a package having a subpackage with the same simple name as a top level type (§7.6) declared in that package. There is no special access relationship between a package named oliver and another package named oliver.twist, or between packages named evelyn.wood and evelyn.waugh.

In the context of your problem, class T cannot access the protected properties of H unless T is a subclass of H.

like image 110
Vineet Reynolds Avatar answered Dec 31 '25 16:12

Vineet Reynolds