wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes overriding Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "overriding" Watch "overriding" New topic
Author

overriding

Shiva Mohan
Ranch Hand

Joined: Jan 05, 2006
Posts: 477
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

Joined: Feb 07, 2005
Posts: 2367
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

Joined: Feb 20, 2007
Posts: 206
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

Joined: Feb 07, 2005
Posts: 2367
Yes you would need to either use the keyword super or have a getter method in the superclass.
abi hari
Greenhorn

Joined: Jan 23, 2007
Posts: 6
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

Joined: Feb 07, 2005
Posts: 2367
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

Joined: Feb 22, 2007
Posts: 285
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

Joined: Apr 12, 2007
Posts: 49
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

Joined: Feb 07, 2005
Posts: 2367
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

Joined: Mar 01, 2001
Posts: 233

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

Joined: Feb 07, 2005
Posts: 2367
That's right.

The declaration of x in the superclass is hidden by the declaration of x in the subclass.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: overriding