aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes protected variables 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 "protected variables" Watch "protected variables" New topic
Author

protected variables

Saroja Eshwar
Greenhorn

Joined: Oct 26, 2007
Posts: 10
As for as i know the protected variables(say protected int x=10 are visible in subclasses of other packages also... So now my question is how the protected variable(x) behave in subclass??

it will be again protected variable or private variable?? I read in Kathy Seira its Private But how is that possible??

Thanks,
Saroja.
greg buela
Ranch Hand

Joined: Sep 04, 2007
Posts: 71
Mmmm... I don't know about what you read by Kathy Sierra, but the variable is still protected. Try it out. Protected variables are visible in subclasses and sub-subclasses.


SCJP 1.5
Kelvin Chenhao Lim
Ranch Hand

Joined: Oct 20, 2007
Posts: 513
I assume that you're referring to the discussion on page 36 of K&B (SCJP 5). What they're talking about is this situation:


In the above code fragment, package2.Child inherits the protected instance variable x from package1.Parent. This inherited x variable is NOT visible to other classes in package2 (hence the error in Neighbor's foo() method), so in that sense it's no longer "protected". Contrast this to if you declare a new "protected int x" in Child, which would be visible to Neighbor. So in this sense x is now "private" with respect to all other classes outside package1.

Admittedly, the paragraph in K&B that explains this is confusing, largely because of their use of the term "private". When they say "private" there, they're not using it in exactly the same sense as the usual Java "private" modifier. (This is why the paragraph also uses the phrase "essentially private" to describe the concept.)
[ October 30, 2007: Message edited by: Kelvin Lim ]

SCJP 5.0
Saroja Eshwar
Greenhorn

Joined: Oct 26, 2007
Posts: 10
ok i agree that it will be still protected variable in subclass..

now my question is if that variable is a protected member it should be visible to all the classes in the subclass package rite??
Kelvin Chenhao Lim
Ranch Hand

Joined: Oct 20, 2007
Posts: 513
Originally posted by Saroja Eshwar:
ok i agree that it will be still protected variable in subclass..

now my question is if that variable is a protected member it should be visible to all the classes in the subclass package rite??


Hi Saroja,
My example above should clarify this.
Saroja Eshwar
Greenhorn

Joined: Oct 26, 2007
Posts: 10
Thanks Kelvin.

The example was very clear... I was referring to the same example...Now it makes sense..

So now we cannot say that variable(x) acts like neither private nor protected..

Actually i got confused with that particular line which you mentioned.. Now I'm clear..
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: protected variables