• 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 ...
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
  • Piet Souris
  • Himai Minh

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

That's my roommate. He's kinda weird, but he always pays his half of the rent. And he gave me this tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
    Bookmark Topic Watch Topic
  • New Topic