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