In the example below, I am confused as to why upcasting seems to refer to some parts of the parent class and some parts of the actual class.
public class B extends A{
int fi = 15;
public static void main(String[] args){
B b = new B();
b.fi = 20;
System.out.println(b.fi);
System.out.println( ( (A) b ).fi );
System.out.println( ( (A) b ).getNum());
}
public int getNum(){
return fi;
}
}
class A{
final int fi = 5;
public int getNum(){
return fi * 2;
}
The printed results are:
20
5
20
I know this code is written in some inefficient ways, but it's similar to an OCA practice question I've gotten. I would like to know why ((A)b).fi refers to the variable in A, but that ((A)b).getNum() uses the variable and method in B. If upcasting refers to the parent, shouldn't the results be 20 5 10
instead?