class Super
{ int index = 5;
public void printVal()
{ System.out.println( "Super" );
}
}
class Sub extends Super
{ int index = 2;
public void printVal()
{ System.out.println( "Sub" );
}
}
public class Runner
{ public static void main(
String argv[] )
{ Super sup = new Sub();
System.out.print( sup.index + "," );
sup.printVal();
}
}
When I run the above code I expected the output to be 2,Sub because sup is a object of Sub class at runtime.But the output is 5,Sub.Even though index is not a static variable,Super class index is printed.Can someone explain the reason behind this?