Ishan Pandya wrote:A vague thing but I think that is actually the difference between how to use a protected members and public members in a subclass of different package.
A public members can be accessed with the (.) operator from anywhere.
IMO, there is nothing vague about this... it is a clear and simple definition. It is somewhat compounded by stuff that makes it a little murky, but each item is also clearly defined in the
Java Language Specification.
The definition is simple. A protected member is a member that is accessible by subclass implementations. And like the other access modifiers, it is at a class level (no worries about instances). A very simple definition.
As for the items that make it murky...
There is the default access level, which means accessible by the same package. This level is also sometimes called package private. This access level is between private and protected -- we can actually debate whether this is the correct position for it, but that's a side issue, it is where it is. Since the protected access level is less restrictive than the default access level, this means that protected members can be accessed by the classes in the same package of the class where the member is defined. In other words, the protected access level also allows the default access level.
The other item is the concept of "responsible for the implementation". This is needed because the access level is based on class, not instances. So, there needs to be a way to determine access when the implementation is using a reference to access another instance. The designers chosen the concept that a class can only access a protected member of a super class only if the instance is of type of its class or subclass (ie. responsible for the implementation). This is also a bit murkier since there is no way to determine the actual instance at compile time, so it just uses the reference type (and type checks at runtime).
Henry