JVM Knowledge

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:

  1. If m_R is marked ACC_PRIVATE, then it is the selected method.

  2. Otherwise, the selected method is determined by the following lookup procedure:

    • If C contains a declaration of an instance method m that can override m_R (§5.4.5), then m is the selected method.

    • Otherwise, if C has a superclass, a search for a declaration of an instance method that can override m_R is 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 not abstract, 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).