Before the dynamic dispatch of method invocation could come into action, the code has to go through the compiler. When you invoke a method using a reference of a class C
, compiler looks out for declaration of that method in that class C
. Compiler only worries about what the reference type it is. It can only validate a method invocation on that much information. If it can't find the method in class C
it will give you compiler error.
So, for the invocation:
a.sayGoodBye();
Since a
is an reference of class A
, compiler will look for the method in class A
, and if it cannot find, it will give compiler error.