• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

overriding

 
Shiva Mohan
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
jiris mock question


i was expecting the answer x=10 as member variables are shadowed.so it only takes access of reference variable type.but it printed 20.
the output really hurts my brain.
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The reference to x in Chapter8 is by its simple name, so it refers to the instance variable x in Chapter8 in this case.
 
megha joshi
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The reference to x in Chapter8 is by its simple name, so it refers to the instance variable x in Chapter8 in this case.


I agree with that, but what would I write if I needed super's x.

Would it be something like this in Chapter8
int x = super.x

Is there any other way?
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes you would need to either use the keyword super or have a getter method in the superclass.
 
abi hari
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi keith,

what does it mean by 'referring to the variable by its simple name'? if this is the case, then in what kind of situation does varible shadowing occur?

Thanks in advance.
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A simple name is a single identifier, like x.

A qualified name is an expression followed by a dot followed by a single identifier, like super.x.
 
raj malhotra
Ranch Hand
Posts: 288
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HI
In the above program ans x=20 is correct.


In the above line as we know that at runtime because of overriding chapter 8 test method will run.As s reference variable contains the reference of Chatpter8 object.so when this program will run x=20 will shadow the x=10 from superclass and we will get x=20 only.
 
m ali
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks guys,



Can anybody explain me the way how to access the 'x' at line5 using Chapter8 object only? I tried it with 'super.super.x' but the compiler slapped me.

Thanks in advance.
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Basically, you can't do it directly. You would have to use a getter method.
[ April 19, 2007: Message edited by: Keith Lynn ]
 
Richard Boren
Ranch Hand
Posts: 233
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by: Shiva Mohan


Sorry about reposting of the code, but couldn't think of a better way.

Is x being shadowed? Both the x in Chapter8 and in Super are instance variables so don't they both have the same scope? From K&B 1.5 p.256; "Showding occurs when two variables with different scopes share the same name."

Richard
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's right.

The declaration of x in the superclass is hidden by the declaration of x in the subclass.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic