• 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

Variable shadowing in inherited method ?

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello all,
Please can anyone help me out in this:-




The output is "Animal Animal"

I want to ask if the name field in Animal is private , it cannot be inherited by Dog class, but the getName() method will be inherited. And on call to getName() method , why does it use a "non-inherited variable" instead of the local object instance variable ?
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Swaraj Pal wrote:
I want to ask if the name field in Animal is private , it cannot be inherited by Dog class, but the getName() method will be inherited. And on call to getName() method , why does it use a "non-inherited variable" instead of the local object instance variable ?



This seems to be a common mistake -- not sure why though... Inheriting a method from the superclass just means that the subclass will use the superclass version of the method. It doesn't mean that the subclass get a copy of the method, which is then recompiled with the scope of the subclass.

Since the subclass is merely using the superclass version of the method, and variables do not support polymorphism, why do you think that the behavior would change?

Henry
 
Swaraj Pal
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Henry Wong wrote: Inheriting a method from the superclass just means that the subclass will use the superclass version of the method. It doesn't mean that the subclass get a copy of the method, which is then recompiled with the scope of the subclass.

Henry



Thanks Henry. That was the problem where I was wrong. I was mistaken of the subclass having a copy of that method.
 
It's just a flesh wound! Or a tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic