• 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
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

overriding

 
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes you would need to either use the keyword super or have a getter method in the superclass.
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 294
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 233
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
moose poop looks like football shaped elk poop. About the size of this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic