public class Quiz { public static void main(String[] args) { A aa = new A(); B bb = new B(); aa.f(); bb.f(); } } class A { public void f() { System.out.printf(“AAAA\n”); } } class B extends A { public void f(int i) { System.out.printf(“BBBB\n”); } }
Look very simple. You may think result is:
AAAA
BBBB
Then, it’s wrong.
B extends A, and has a f() method as A. You may think it’s
“overriding”, but be careful, the f() method in B has a parameter “int
i”. So in fact it’s not an “overriding”, but a “overloading” of B’s
method f() — Override need same parameter(s).
In the main, we called aa.f() and bb.f(). For aa.f(), easy, just output “AAAA”.
But for bb.f(), this call doesn’t have a parameter. so in fact it’s trying to call the f() which is extended from A.
So the result is:
AAAA
AAAA