Friday, 23 August 2013

Can somebody please explain me this output of polymorphism?

Can somebody please explain me this output of polymorphism?

Heres the code :
class A{
int data=10;
void show1(){
System.out.println("In A="+data);
}
}
class B extends A{
int data=20;
void show2(){
System.out.println("In B="+data);
}
}
public class Overriding4 {
public static void main(String args[]){
B b=new B();
System.out.println("Data1="+b.data);
System.out.println("Data2="+b.data);
b.show1();
b.show2();
}
}
And heres the output :
Data1=20
Data2=20
In A=10
In B=20
The output at Data1=20 should be 10 , not 20...but I think I'm missing
something here. Please help me with this

No comments:

Post a Comment