• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

protected variables

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 513
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Saroja Eshwar
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 513
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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..
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic