• 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

Regarding Inheritance

 
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hello all ranchers,

while solving Rule Roundup game i came across this problem:-

The answer is :- Akshay

well my doubt is when method call() is called at 1 ,since subclass doesn't override call() method the superclass call() method is called.That is ok.
Also the reference of the subclass is also passed implicitly to the method.
So i can correctly rewrite the code at 2 as:
public void call(){System.out.println(this.s);}

Then why doesn't "Singhvi" gets printed.

Hope i must have clearly jotted down my doubt
[ November 20, 2005: Message edited by: Barry Gaunt ]
 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Akshay,

Good question....

The case here is of "Member Hiding". In your subclass you have hidden String object of Superclass by giving the same name. But in JAVA, only methods can be overridden and not the attributes. So, your superclass doesnt have any idea about Overridden String. When the control goes into the superclass, it just know its own copy of the variable. So it prints like that.

Again, want to quote one thing "Methods can be overridden, not the attributes".

Hope it will help

Regards,
Mausam
 
reply
    Bookmark Topic Watch Topic
  • New Topic