I have been knowing java for a while still sometime i think, i lack few basic concepts. Here is a code which i am trying to run:
class A{
int x =10;
void al(){
System.out.println("From a");
}
}
class B extends A{
int x = 20;
void al(){
System.out.println("From b");
}
public static void main(String args[]){
A obj = new B();
B obj1 = new B();
A obj2 = new A();
System.out.println(obj.x);
obj.al();
System.out.println(obj1.x);
System.out.println(obj2.x);
}
}
and here is the output:
10
From b
20
10
When i try to call the function al it calles the function in B class. Simple overriding is happening over here. Obj tries to find first in base class then in super class. Now, when i try to access the "X" by object obj it displays "10". Why it is not displaying "20" ?