If more than one member method is both accessible and applicable to a method invocation, it is necessary to choose one to provide the descriptor for the run-time method dispatch. The Java programming language uses the rule that the most specific method is chosen.
The informal intuition is that one method is more specific than another if any invocation handled by the first method could be passed on to the other one without a compile-time error. In cases such as an explicitly typed lambda expression argument (§15.27.1) or a variable arity invocation (§15.12.2.4), some flexibility is allowed to adapt one signature to the other.
One applicable method m_1 is more specific than another applicable method m_2, for an invocation with argument expressions e_1, ..., e_k, if any of the following are true:
m_2is generic, andm_1is inferred to be more specific thanm_2for argument expressionse_1, ...,e_kby §18.5.4.m_2is not generic, andm_1andm_2are applicable by strict or loose invocation, and wherem_1has formal parameter types S_1, ..., S_n andm_2has formal parameter types T_1, ..., T_n, the type S_i is more specific than T_i for argumente_ifor all i (1 ≤ i ≤ n, n = k).m_2is not generic, andm_1andm_2are applicable by variable arity invocation, and where the first k variable arity parameter types ofm_1are S_1, ..., S_k and the first k variable arity parameter types ofm_2are T_1, ..., T_k, the type S_i is more specific than T_i for argumente_ifor all i (1 ≤ i ≤ k). Additionally, ifm_2has k+1 parameters, then the k+1'th variable arity parameter type ofm_1is a subtype of the k+1'th variable arity parameter type ofm_2.
The above conditions are the only circumstances under which one method may be more specific than another.
A type S is more specific than a type T for any expression if S <: T (§4.10).
A functional interface type S is more specific than a functional interface type T for an expression e if all of the following are true:
-
The interface of S is neither a superinterface nor a subinterface of the interface of T.
If S or T is an intersection type, it is not the case that any interface of S is a superinterface or a subinterface of any interface of T. (The "interfaces of" an intersection type refers here to the set of interfaces that appear as (possibly parameterized) interface types in the intersection.)
-
Let MT_S be the function type of the capture of S, and let MT_T be the function type of T. MT_S and MT_T must have the same type parameters (if any) (§8.4.4).
-
Let P_1, ..., P_n be the formal parameter types of MT_S, adapted to the type parameters of MT_T. Let P_1', ..., P_n' be the formal parameter types of the function type of S (without capture), adapted to the type parameters of MT_T. Let Q_1, ..., Q_n be the formal parameter types of MT_T. Then, for all i (1 ≤ i ≤ n), Q_i
<:P_i and Q_i = P_i'.Generally, this rule asserts that the formal parameter types derived from S and T are the same. But in the case in which S is a wildcard-parameterized type, the check is more complex in order to allow capture variables to occur in formal parameter types: first, each formal parameter type of T must be a subtype of the corresponding formal parameter type of the capture of S; second, after mapping the wildcards to their bounds (§9.9), the formal parameter types of the resulting function types are the same.
-
Let R_S be the return type of MT_S, adapted to the type parameters of MT_T, and let R_T be the return type of MT_T. One of the following must be true:
-
eis an explicitly typed lambda expression (§15.27.1), and one of the following is true:-
R_T is
void. -
R_S
<:R_T. -
R_S and R_T are functional interface types, and there is at least one result expression, and R_S is more specific than R_T for each result expression of
e.The result expression of a lambda expression with a block body is defined in §15.27.2; the result expression of a lambda expression with an expression body is simply the body itself.
-
R_S is a primitive type, and R_T is a reference type, and there is at least one result expression, and each result expression of
eis a standalone expression (§15.2) of a primitive type. -
R_S is a reference type, and R_T is a primitive type, and there is at least one result expression, and each result expression of
eis either a standalone expression of a reference type or a poly expression.
-
-
eis an exact method reference expression (§15.13.1), and one of the following is true:- R_T is
void. - R_S
<:R_T. - R_S is a primitive type, R_T is a reference type, and the compile-time declaration for the method reference has a return type which is a primitive type.
- R_S is a reference type, R_T is a primitive type, and the compile-time declaration for the method reference has a return type which is a reference type.
- R_T is
-
eis a parenthesized expression, and one of these conditions applies recursively to the contained expression. -
eis a conditional expression, and, for each of the second and third operands, one of these conditions applies recursively.
-
A method m_1 is strictly more specific than another method m_2 if and only if m_1 is more specific than m_2 and m_2 is not more specific than m_1.
A method is said to be maximally specific for a method invocation if it is accessible and applicable and there is no other method that is accessible and applicable that is strictly more specific.
If there is exactly one maximally specific method, then that method is in fact the most specific method; it is necessarily more specific than any other accessible method that is applicable. It is then subjected to some further compile-time checks as specified in §15.12.3.
It is possible that no method is the most specific, because there are two or more methods that are maximally specific. In this case:
-
If all the maximally specific methods have override-equivalent signatures (§8.4.2), and exactly one of the maximally specific methods is concrete (that is, neither
abstractnor default), then it is the most specific method. -
Otherwise, if all the maximally specific methods have override-equivalent signatures, and all the maximally specific methods are
abstractor default, and the declarations of these methods have the same erased parameter types, and at least one maximally specific method is preferred according to the rules below, then the most specific method is chosen arbitrarily among the subset of the maximally specific methods that are preferred. The most specific method is then considered to beabstract.A maximally specific method is preferred if it has:
- a signature that is a subsignature of every maximally specific method's signature; and
- a return type R (possibly
void), where either R is the same as every maximally specific method's return type, or R is a reference type and is a subtype of every maximally specific method's return type (after adapting for any type parameters (§8.4.4) if the two methods have the same signature).
If no preferred method exists according to the above rules, then a maximally specific method is preferred if it:
- has a signature that is a subsignature of every maximally specific method's signature; and
- is return-type-substitutable (§8.4.5) for every maximally specific method.
The thrown exception types of the most specific method are derived from the
throwsclauses of the maximally specific methods, as follows:-
If the most specific method is generic, the
throwsclauses are first adapted to the type parameters of the most specific method (§8.4.4).If the most specific method is not generic but at least one maximally specific method is generic, the
throwsclauses are first erased. -
Then, the thrown exception types include every type E which satisfies the following constraints:
- E is mentioned in one of the
throwsclauses. - For each
throwsclause, E is a subtype of some type named in that clause.
- E is mentioned in one of the
These rules for deriving a single method type from a group of overloaded methods are also used to identify the function type of a functional interface (§9.9).
-
Otherwise, the method invocation is ambiguous, and a compile-time error occurs.