• 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

Can you access inherited protected data members in another instance of the same class?

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am using the Sybex SCJP book to study for the 1.5 exam. Last night I did one of their mock exams on the CD and got a question wrong because, so the answer said, you "cannot access protected superclass data of a different instance".

The specific context was a class a.X extends b.Y, and b.Y has a protected instance variable called 'size'. The question asked if you could use the expression new X().size within X's class definition. Note that 'size' was not itself redeclared within the class definition so it was available to X only through inheritance. I said yes, but the answer said no.

I have since checked this within Eclipse and it seems that you CAN use the expression. Can anyone confirm exactly what the rule is here? Probably more importantly, I guess the Sybex book was trying to make a point here and although they have made it inaccurately can anyone suggest exactly what point they might have been trying to make?

Any assistance appreciated!
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
They might have tried (and failed) to explain that if c.Z is another class that extends b.Y, then code in a.X cannot access "size" in an instance of c.Z, or, more generally, any instance of b.Y that is not known at compile time to be a.X -- i.e., this is illegal

// Code is in a.X
b.Y parent = new a.X();
int illegal = parent.size;
 
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can always access class members in another instance of the same class.

And welcome to the Ranch
 
Nige Wheeler
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Ernest and Campbell for your help. Worrying that the book makes such a fundamentally incorrect statement, but at least it was the author that was wrong rather than me so I must have been doing something right in my studies! Maybe I'll see if Sybex has a way to report errata like this.

Looking forward to more discussions like this on the ranch. Even though I only joined yesterday I can already see what a great site it is. Yeehah!
 
A lot of people cry when they cut onions. The trick is not to form an emotional bond. This tiny ad told me:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic