I have two packages in my project: odp.proj
and odp.proj.test
. There are certain methods that I want to be visible only to the classes in these two packages. How can I do this?
EDIT: If there is no concept of a subpackage in Java, is there any way around this? I have certain methods that I want to be available only to testers and other members of that package. Should I just throw everything into the same package? Use extensive reflection?
You can't. In Java there is no concept of a subpackage, so odp.proj and odp.proj.test are completely separate packages.
As others have explained, there is no such thing as a "subpackage" in Java: all packages are isolated and inherit nothing from their parents.
An easy way to access protected class members from another package is to extend the class and override the members.
For instance, to access
ClassInA
in packagea.b
:make a class in that package that overrides the methods you need in
ClassInA
:That lets you use the overriding class in place of the class in the other package:
Note that this only works for protected members, which are visible to extending classes, and not package-private members, which aren't. Hopefully this helps someone!