I'm puzzled and a little bit wasted by the next snippet. I guess it is all because of static inner class and scope of the function of class A1 which is called.
If you have a explanation-in-details, please share it!
public class Main {
static class A1 {
private void f() { System.out.println("A1");}
}
static class A2 extends A1 {
public void f() {System.out.println("A2");}
}
static class A3 extends A2 {
public void f() {System.out.println("A3");}
}
public static void main(String[] args) {
A1 a1 = new A1();
a1.f();
a1 = new A2();
a1.f();
a1 = new A3();
a1.f();
}
}
Expected:
A1
A2
A3
Actual:
A1
A1
A1