During execution of an invokeinterface or invokevirtual instruction, a method is selected with respect to (i) the run-time type of the object on the stack, and (ii) a method that was previously resolved by the instruction. The rules to select a method with respect to a class or interface C and a method m_R are as follows:
-
If
m_Ris markedACC_PRIVATE, then it is the selected method. -
Otherwise, the selected method is determined by the following lookup procedure:
-
If C contains a declaration of an instance method
mthat can overridem_R(§5.4.5), thenmis the selected method. -
Otherwise, if C has a superclass, a search for a declaration of an instance method that can override
m_Ris performed, starting with the direct superclass of C and continuing with the direct superclass of that class, and so forth, until a method is found or no further superclasses exist. If a method is found, it is the selected method. -
Otherwise, the maximally-specific superinterface methods of C are determined (§5.4.3.3). If exactly one matches
m_R's name and descriptor and is notabstract, then it is the selected method.Any maximally-specific superinterface method selected in this step can override
m_R; there is no need to check this explicitly.
-
While C will typically be a class, it may be an interface when these rules are applied during preparation (§5.4.2).