my dog learned polymorphism*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Old Chestnut: Protected Access Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Old Chestnut: Protected Access" Watch "Old Chestnut: Protected Access" New topic
Author

Old Chestnut: Protected Access

Michal Charemza
Ranch Hand

Joined: Jul 13, 2004
Posts: 86
Hi,

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?

Public
Protected
default (or package level)
"private-but-can-be-inherited" (an inherited protected member)
Private

Any help would be greatly appreciated.

Mikey
Gian Franco
blacksmith
Ranch Hand

Joined: Dec 16, 2003
Posts: 977
Hi,

Given the following class myClassA:

and myClassB


A compilation error will occur, because the protected modifier's
effects of myMethodA() propagates through it's child classes, unless
you override the method, like:



K&B indicates the visibility of protected members is:

Package and kids

So, there are only four access modifiers, from a given
class you have to trace back where the method has been
declared.

Cheers,

Gian Franco


"Eppur si muove!"
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
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.

I can't think of a single situation that counters that. You can read more about proteceted access in the JLS, §6.6.2 Details on protected Access.


SCJP Tipline, etc.
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Mikey Mike,

Welcome to Javaranch

We'd like you to read the Javaranch Naming Policy and change your publicly displayed name (change it here) to comply with our unique rule. Thank you.
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093
Protected members do not become private on inheritance (I think C++ does something like that somewhere, but Java does not).

They remain protected.


42
Michal Charemza
Ranch Hand

Joined: Jul 13, 2004
Posts: 86
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"?

Thanks

Michal
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
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.
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093
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.
Michal Charemza
Ranch Hand

Joined: Jul 13, 2004
Posts: 86
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.

Michal
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Old Chestnut: Protected Access