I'm trying to understand the "protected" access modifier in Java. My book says that a protected method/variable can be access by any class in the same package AND by any class that extends the class that contains the protected feature. That makes sense to me...
Why then does class Swan (below) NOT compile? I thought it should compile, since it's accessing the protected variable from an instance of a class that extends the owner of the protected feature...
You can't "two-step" protected access. In other words, you can have one class inherit a protected method then try to have another class outside the package inherit it again. The buck stops here, so to speak, at the first inheritance level. Make sense? If not, I'll elaborate after I get my second cup of coffee!
Give a man a fish, he'll eat for one day. Teach a man to fish, he'll drink all your beer.
Cheers, Jeff (SCJP 1.4, SCJD in progress, if you can call that progress...)
Joined: Oct 07, 2004
Yup that makes sense... Thanks for the fast reply!
This is a difficult concept to grasp without going slowly.
Check out this chart:
Basically, a class has complete access to all members and methods.
a subclass has access to protected and public members.
A class in the same package has similar access privledges.
Everyone else only has access to the public menbers and methods.
Here is your Swan class:
It has extended Bird and according to the chart above, it has access to the protected variable nFeathers.
However, you have also created a new object Duck1.
Here you will only be have access to protected members of type Swan.
So while this is illegal:
This would be perfectly legal
Why? Because you are accessing a protected member of type Swan here and you are trying to access a protected member of type Duck1 in the previous statement.
Make sense? Or did I just ramble on endlessly here?
(here is an afterthought: it's overly simplified but you can think of it like this...Your swan has feathers and your swan has a buddy that happens to be a duck. Your swan can modify it's own feathers but it cannot modify the feathers of it's buddy) [ November 08, 2004: Message edited by: jefff willis ]
Joined: Oct 07, 2004
Hmmmm your explanation got me thinking...
So in this situation, it doesn't matter what classes I instantiate within the class Swan, it's the Swan class that determines what features are accessible from within that class.
So the instance of Duck1 in the Swan class must see things through Swan's eyes (right?)
I dont' know if I'm articulating it clearly but I think I'm understanding this better... You're right though, it isn't an easy concept to understand...
Joined: Sep 29, 2004
Yeah, it's real easy to get lost in the terms and concepts with this.
I think you are on the right track though.
Here is some additional information that may help:
We've already established that your swan may not directly modify the ducks feathers like this:
and that the swan is free to modify it's own feathers like this:
However if the duck wanted to give permission for birds to modify it's feathers, the Duck class would have to add this method:
And then the swan could modify the ducks feathers like this:
Again, this is overly simplified. Eventually you will have to describe these same concepts in terms of "inheritance" and "encapsulation".
In more strict terms: "A protected member or constructor of an object may be accessed from outside the package in which it is declared only by code that is responsible for the implementation of that object."