I'm fairly new to programming and do not understand why this code prints 200 instead of 206. The move method in class Cat overrides the move method in class Animals. Why does the 'location' instance variable in Animals not change to 206 after the method call on line 2? However, when I remove the method in class Cat, then the instance variable DOES change to 206. What is the logic behind it?
public class Animals {
int location = 200; //line 1
public void move(int by) {
location = location+by;
}
public final static void main (String...args) {
Animals a = new Cat();
a.move(6); //line 2
System.out.println(a.location); //200, but should print 206 in my opinion
}
}
class Cat extends Animals {
int location = 400;
@Override
public void move(int by) { //if this method is removed, a.location prints 206
location = location+by;
}
}