Why can't subpackages see private package classes?

So, I have this project structure:

AB package

  • class SuperClass (this class is marked as private)

ABC package

  • class SubClass (inherited from the superclass)

I would rather not make SuperClass public ... It's really just a utility class for this particular project (AB).

It seems to me that SubClass should be able to see SuperClass , because the ABC package is a subpackage of AB .. but it is not.

What would be the best way to solve this problem? I don’t think it makes sense to move everything in ABC to AB or move AB to ABC .. mainly because there will probably be an ABD that inherits from things in AB, as well ...

I'm a little new to Java, so be nice: D (I'm a C ++ and .NET guy)

+7
java inheritance packages organization
source share
3 answers

Packages are unique identifiers. You cannot force them to follow the rules of inheritance. The package and subpackages are not similar to the Super and Sub classes.

I do not see any flaws in the fact that the class that you want to use in the additional package is visible to the outside world. I would be interested to know how this criterion is processed in C ++ /. NET (since I am a java guy :))

+9
source share

It is best to declare the default SuperClass constructor as protected . Thus, only instances in the same package and subclasses, regardless of the package, can instantiate and extend it.

+6
source share

Why not put them on the same level?

Could you use composition instead of inheritance? I tried to do this on my own after reading Effective Java. Not sure if this is possible given your needs, but it might be worth considering.

Good luck.

-one
source share

All Articles