How to make data member access only available for class and subclass

Possible duplicate:
Why is there no subclass visibility modifier in Java?

The access level table for Java shows 4 different parameters for controlling access to class members:

Modifier Class Package Subclass World public YYYY protected YYYN no modifier YYNN private YNNN 

There is no modifier, however, for "accessible only for class and subclass". I.e:

 Modifier Class Package Subclass World c++prot YNYN 

Is it even possible to define such a level of access in Java?

If so, how?

If this is not possible, it should be associated with a well-designed design. If so, what is this principle. In other words, why is having such a level of access in Java not a good idea?

+4
source share
2 answers

The design principle is that developers do not maliciously crack their own programs. If you do not trust the classes in the same package to behave correctly, you have serious non-technical problems IMHO.

Access modifiers are designed to limit random errors, and they try to stop most errors with minimal complexity.

BTW: with reflection / JNI calls, you can bypass all access modifiers, so they are not a reliable IMHO security measure.

+8
source

C ++ has no concept of built-in packages, so when comparing access modifiers between C ++ and Java, you should ignore the "Package" column :)

+2
source

Source: https://habr.com/ru/post/917196/


All Articles