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();
}
}
The output is 5 and sub.
I thought its 2 and sub.
Super class ref. sup -> sub class obj.
sup.printval() calling sub class method & printing "sub". Iam clear abt that.
sup.index() :
If its the same case
sup will call sub class index var. Don't they? .
Then why its 5 Not 2.
plz help me with this/
thanks