The following code when run obviously prints out "B1/A2/B2". Now, is it possible for it to print "A1/A2/B2" instead (i.e. A#method2() should invoke method1() on A, not on B)?
Note: I have no such need to get pass polymorphism, this question is out of curiosity only.
class A {
public void method1() {
System.out.println("A1");
}
public void method2() {
method1();
System.out.println("A2");
}
}
class B extends A {
@Override public void method2() {
super.method2();
System.out.println("B2");
}
@Override public void method1() {
System.out.println("B1");
}
}
public class Tmp {
public static void main(String args[]) {
B b = new B();
b.method2();
}
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…