This week's book giveaway is in the OCPJP forum. We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line! See this thread for details.
I think I understand that a member method/variable marked "protected" is accessible (through a reference) by classes in the same package, not accessible (through a reference) by classes in a different package, and is inherited by subclasses regardless of which package they belong to. (If I'm wrong please correct me)
I am confused about the level of access of that a "protected" member has once it has been inherited by a subclass.
Is it true that an inherited member, that was "protected" in its superclass, is "private" in the subclass, apart from the fact that it can be inherited by subclasses (of the subclass) regardless of which package they belong to?
Or does the subclass (of the subclass) have to be in the same package as the subclass to inherit the inherited "protected" member?
Also (and this is getting a bit confusing for me... well... a bit more confusing anyway) what access level does that member have once it has been inherited by a subclass of the subclass of the original class that had the "protected" member? Is it again the special "private"-but-can-be-inherited"?
Does this all mean that there are in fact 5 levels of access in Java?
I think it's important to remember that a protected member variable is accessible to any subclasses of the class in which it is defined (regardless of the package the subclass is in) and the package in which it is defined.
That means that any class in the same package can access the variable, even if it's through a subclass within that package. This code prints 10:
Now, if that doesn't rattle your noodle, try this on for size:
So what do you think that does? Well, it prints 13, of course.
The member variable, a, is defined in package one. Therefore, the class OtherClassInOne, has access to it, even though it is accessing it via a class in another package, two.Sub.
So protected members are accessible in two situations:
1. By any class that extends the class in which it is defined. 2. By any class defined in the same package in which it is defined.
Protected members do not become private on inheritance (I think C++ does something like that somewhere, but Java does not).
They remain protected.
Joined: Jul 13, 2004
So if a class, say classSub, extends a class, classSuper, which has a protected member, memberProtected, does memberProtected "act" as though it was defined in classSub with an access level of "protected"?
Joined: Dec 20, 2001
Originally posted by Mikey Mike: So if a class, say classSub, extends a class, classSuper, which has a protected member, memberProtected, does memberProtected "act" as though it was defined in classSub with an access level of "protected"?
No, it still "acts" as if it was defined where it was originally defined. Adding on to the example I used earlier, if we try to compile this:
Compiling this would result in a compiler error because a is inaccessable.
Joined: Oct 12, 2000
the inherited member never leaves the class it was defined in, again unlike some other languages where the new class contains all inherited members of the parent class. So the exact same visibility it had in the parent still applies, including package access rules applicable to the parent class.
Treat inherited members as living in an instance of the parent class which sits invisibly behind the inherited class instance and you won't be far off from what's really happening.
Joined: Jul 13, 2004
Originally posted by Jeroen Wenting: So the exact same visibility it had in the parent still applies, including package access rules applicable to the parent class.
Thank you! This is exactly what I was looking for.