public class AbcMain {
public static void main(String[] args) {
Parent p = new Child();
p.sayHello();
System.out.println(p.a);
}
}
class Parent{
int a = 10;
public void sayHello(){
System.out.println("Hello inside parent.");
}
}
class Child extends Parent{
int a = 20;
public void sayHello(){
System.out.println("Hello inside child. ");
}
}
Output is : Hello inside child. 10
Confused here, It is calling the method of Child() as the instance is of child. Then why it prints a = 10?
int getA(), Then override method inChildvlass