I am aware of the concept called field hiding
in java. But still I am having a confusion in relation to instance variable being not over-ridden.
According to my present knowledge, overriding a method of super-class means that the JVM will call the sub-class's over-ridden method though the super-class's method is available to the sub-class.
And I read the similar thing for field hiding
via the link:- Hiding Fields
So, in any case we are over-ridding the instance if we change the values of the inherited instance variable in the sub-class.
I am confused please help.
I am using the following super-class:-
public class Animal{
File picture;
String food;
int hunger;
int width, height;
int xcoord, ycoord;
public void makeNoise(){
.........
}
public void eat(){
.............
}
public void sleep(){
..........
}
public void roam(){
.............
}
}
It has sub-classes like Tiger, cat, dog,hippo etc. The sub-classes over-ride the makeNoise(), eat and roam() method.
But each sub-class also uses a different set of values for instance variables.
So as per my confusion, I am kind-of overriding all the instance variables and 3 methods of the super-class Animal; and I still have the super-class instance variables available to the sub-class with the use of the super
keyword.
It means if you call a method in a superclass it will resolve to the overriden version in a subclass (if your instance is a subclass). However a reference to a member variable will be bound to the declaration of that variable in the class from which the call is made.