Hi
As per java, instance method resolution is based on runtime types of the arguments.
But while resolving instance variable it uses different approach as shown below.
Output of program is ..
Child
Parent
ParentNonStatic
Here First output is based on runtime types of the argument but third output is not.
can any one explain about this ?
public class Child extends Parent {
public static String foo = "Child";
public String hoo = "ChildNonStatic";
private Child() {
super(1);
}
@Override
String please() {
return "Child";
}
public static void main(String[] args) {
Parent p = new Parent();
Child c = new Child();
//Resolving method
System.out.println(((Parent) c).please());
//Resolving Static Variable
System.out.println(((Parent) c).foo);
//Resolving Instance Variable
System.out.println(((Parent) c).hoo);
}
}
class Parent {
public static String foo = "Parent";
public String hoo = "ParentNonStatic";
public Parent(int a) {
}
public Parent() {
}
String please() {
return "Tree";
}
}