If I have two classes, A and B,
public class A {
public int test() {
return 1;
}
}
public class B extends A{
public int test() {
return 2;
}
}
If I do: A a1 = new B(), then a1.test() returns 2 instead of 1 as desired.
Is this just a quirk of Java, or is there some reason for this behavior?
dynamic binding, You need to know about thestatic bindingalso. It gets little trickier when you try to override (infact hide) the fields instead of methods. You will find lot of questions on this on SO or google.