Why does the inherited method show_hp()
of the class Knight doesn't use the class's variable hp, which was redefined in lower-in-hierarchy subclass?
I've tried overriding the method show_hp()
and it worked, but I'm missing the concept of polymorphism, I think.
public class scratch {
public static void main(String[] args) {
Knight Ricardo = new Knight();
Ricardo.show_hp();
}
static class Hero {
int hp = 5;
public void show_hp() {
System.out.println(" I have " + hp + " hp!");
}
}
static class Knight extends Hero {
int hp = 3;
}
}
I expect the output of 3, but the actual one is 5.