How to make data member accessible to class and subclass only [duplicate]
Possible Duplicate:
Why is there no sub-class visibility modifier in Java?
The access level table for Java, shows 4 different options for controlling access to members of a class:
Modifier Class Package Subclass World
public Y Y Y Y
protected Y Y Y N
no modifier Y Y N N
private Y N N N
There is no modifier, however, for "accessible to class and subclass only". That is:
Modifier Class Package Subclass World
c++prot Y N Y N
Is it possible at all to define such access level in Java?
If so, how?
If this isn't 开发者_开发技巧possible, this must be due to a well thought design principle. If so, what is that principle. In other words, why having such access level in Java isn't a good idea?
The design principle is that developers do not maliciously hack their own programs. If you don't trust classes in the same package to behave correctly you have serious non-technical issues IMHO.
The access modifiers are there to restrict accidental errors, and they attempt to stop most error with minimum complexity.
BTW: with reflection/JNI calls you can bypass all access modifiers, so they are not a rock solid security measure IMHO.
C++ does not have the concept of packages built-in so when comparing the access modifiers between c++ and Java you should disregard the 'Package' column :)
精彩评论