My question is the following,
class A {
int i =0;
}
public class B extends A{
int i = 100;
public static void main(
String[] args){
A a = new A(); System.out.println("A:i = " + a.i);
B b = new B(); System.out.println("B:i = " + b.i);
A c = new B(); System.out.println("Wanted i = " + c.i);
}
}
The output is :
A:i = 0
B:i = 100
Wanted i = 0
I feel that at runtime the JVM should have known that the object in variable c is actually a B object and should have printed the value "Wanted i = 100" and not what I got. Somehow
Polymorphism is not working for instance variables, though if the same had happened with methods, the method is B would have been called.
Plz help...