class HelloWorld
{
public static void main(String[] args)
{
A a = new B();
System.out.println(a.getX());
a.show();
}
}
class A {
int x = 10;
public A() {
}
public int getX() {
return x;
}
public void show(){
System.out.println("A show");
}
}
class B extends A{
int x = 20 ;
public B() {
super();
}
@Override
public void show(){
System.out.println("B show");
}
}
Why is the output this?
10
B Show
I understand 'B Show'. Since the object is of class B, it will call the method that is overridden in B. But do not understand why it is printing '10' in output even though the object of B has value of x equal to 20. Please help me understand this.