I have an understanding of variable hiding and method overriding and virtual method calling in Java. My question is, why does variable hiding fail to take effect in inherited methods? Does it mean that we have to override the methods which access those variables in every child class?
Abstract class
public abstract class ClassA{
protected int i = 0;
public void printOurStuff(){
System.out.println(i);
}
public void printMyStuff(){
System.out.println(this.i);
}
public void printSomeStuff(String s){
System.out.println(s);
}
}
Concrete class
public class ClassB extends ClassA{
protected int i = 1;
public static main(String[] args){
ClassB b = new ClassB();
b.printOurStuff();
b.printMyStuff();
b.printSomeStuff(b.i);
}
}
Results
0 0 1
EDIT - changed the access modifier of the field from private to protected and added method printOurStuff