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
Okay , thanks for the help, but one new doubt : What would happen if I changed the main method to :
public static void main(String args[]){
A a=new B();
System.out.println("Data1="+a.data);
System.out.println("Data2="+a.data);
a.show1();
a.show2();
}
There you go.
Unless you made a typo, the two line below are basically the same:
It may be helpful to visualize the object like this: