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.
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.
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: 2341
posted
0
That's right.
The declaration of x in the superclass is hidden by the declaration of x in the subclass.