java - Can somebody please explain me this output of polymorphism? -
heres 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 output :
data1=20 data2=20 in a=10 in b=20
the output @ data1=20 should 10 , not 20...but think i'm missing here. please me this
okay , help, 1 new doubt : happen if changed main method :
public static void main(string args[]){ a=new b(); system.out.println("data1="+a.data); system.out.println("data2="+a.data); a.show1(); a.show2(); }
there go.
class fields don't inherited. note have same int data
field in both classes a
, b
. called hiding.
Comments
Post a Comment