Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: Subclass access without package access

Fairly new to Java, but I'm wondering why package access is considered "more restrictive" than subclass access. That is, every access modifier which provides subclasses with access to a member also provides the whole package with access, and there are modifiers whic provide package access but not subclass access.

Isn't this totally backwards? Let's say I have a class ControlledInstantiation in some package. If I have another class AlsoControlledInstantiation extends ControlledInstantiation, I am unable to call the constructor of ControlledInstantiation unless I set it to protected or public. And if I set it to protected, now any other class in the package can instantiate it as often as it likes. So something which is obliged to be substitutable for its superclass (and, syntactically, is) gets the same or less access to the superclass than something which serves a distinct but related function. It's like telling your child he can't play with your wallet because you wouldn't let your neighbours do it and then letting your neighbours sleep in your house because your kid does.

So I guess I'm asking, what motivated this decision, and how can I get around it?

like image 770
Innominate Avatar asked Nov 21 '10 05:11

Innominate


People also ask

Can a protected field of a class be inherited to subclass outside the package?

We cannot access the protected members of a class in a class (non-subclass) that is present in a different package.

Which field is accessible from outside the package?

Outside the package, the member j is accessible to any class, whereas the member k is only accessible to subclasses of MyClass. The field i has package accessibility, and is only accessible by classes inside the package. The field j has public accessibility, and is accessible from anywhere.

How are protected variables accessible in sub classes belonging to different package?

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.


1 Answers

It may seem backwards at first, but the idea is that a Java package should contain a set of comparatively cohesive classes which are semantically related, and this is reflected in the default package modifier. Then the logic is that if you want to go one step further and allow subclasses from any package to view your members, you can declare them protected. Does it make sense to you that subclasses from foreign packages should be less trusted than any class (whether a subclass or not) from your own package?

Java did in fact once have a private protected modifier which would achieve what you're after, but it was removed, I imagine, because it confused people. I'm not really sure how you could achieve this without relegating each class/subclass pair to its own package. But that's a messy solution which goes against Java's principles and it wouldn't work for inheritance hierarchies of more than two classes anyway.

like image 163
asdfjklqwer Avatar answered Nov 03 '22 00:11

asdfjklqwer