public class Test3 {
public static void main(String[] args) {
Derived04 b = new Derived04();
Base04 a = (Base04)b;
System.out.println(a.i);
System.out.println(a.f());
}
}
class Base04 {
int i=1;
public int f() {return i;}
}
class Derived04 extends Base04 {
int i = 2;
public int f(){return -i;}
}
Both classes include the f() method so when I cast d onto a which method does it go to when I call a.f()? I thinking since a is declared as Base04 type when you do a.i it gives 1 but then why does it use the method from the subclass?