---
spec: JLS
title: "The Java Language Specification"
edition: "Java SE 17 Edition"
java_version: 17
chapter: "Chapter 15. Expressions"
section: "15.12.2.5"
heading: "15.12.2.5. Choosing the Most Specific Method"
parent: "15.12.2. Compile-Time Step 2: Determine Method Signature"
source: https://docs.oracle.com/javase/specs/jls/se17/html/jls-15.html#jls-15.12.2.5
id: "15.12.2.5"
url: https://jvm.obfuscated.software/jls/se17/15.12.2.5.html
raw: https://jvm.obfuscated.software/jls/se17/15.12.2.5.md
---

# 15.12.2.5. Choosing the Most Specific Method

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](15.27.1.md)) or a variable arity invocation ([§15.12.2.4](15.12.2.4.md)), 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_2` is generic, and `m_1` is inferred to be more specific than `m_2` for argument expressions `e_1`, ..., `e_k` by [§18.5.4](18.5.4.md).
- `m_2` is not generic, and `m_1` and `m_2` are applicable by strict or loose invocation, and where `m_1` has formal parameter types S_1, ..., S_n and `m_2` has formal parameter types T_1, ..., T_n, the type S_i is *more specific* than T_i for argument `e_i` for all *i* (1 ≤ *i* ≤ *n*, *n* = *k*).
- `m_2` is not generic, and `m_1` and `m_2` are applicable by variable arity invocation, and where the first *k* variable arity parameter types of `m_1` are S_1, ..., S_k and the first *k* variable arity parameter types of `m_2` are T_1, ..., T_k, the type S_i is *more specific* than T_i for argument `e_i` for all *i* (1 ≤ *i* ≤ *k*). Additionally, if `m_2` has *k*+1 parameters, then the *k+1*'th variable arity parameter type of `m_1` is a subtype of the *k+1*'th variable arity parameter type of `m_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](4.10.md)).

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](8.4.4.md)).

- 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](9.9.md)), 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:

  - `e` is an explicitly typed lambda expression ([§15.27.1](15.27.1.md)), 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](15.27.2.md); 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 `e` is a standalone expression ([§15.2](15.2.md)) 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 `e` is either a standalone expression of a reference type or a poly expression.

  - `e` is an exact method reference expression ([§15.13.1](15.13.1.md)), 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.

  - `e` is a parenthesized expression, and one of these conditions applies recursively to the contained expression.

  - `e` is a conditional expression, and, for each of the second and third operands, one of these conditions applies recursively.

  - `e` is a `switch` expression, and, for each of its result expressions, 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](15.12.3.md).

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](8.4.2.md)), and *exactly one* of the maximally specific methods is concrete (that is, neither `abstract` nor 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 `abstract` or 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 be `abstract`.

  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](8.4.4.md)) 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](8.4.5.md)) for every maximally specific method.

  The thrown exception types of the most specific method are derived from the `throws` clauses of the maximally specific methods, as follows:

  1. If the most specific method is generic, the `throws` clauses are first adapted to the type parameters of the most specific method ([§8.4.4](8.4.4.md)).

     If the most specific method is not generic but at least one maximally specific method is generic, the `throws` clauses are first erased.

  2. Then, the thrown exception types include every type E which satisfies the following constraints:

     - E is mentioned in one of the `throws` clauses.
     - For each `throws` clause, E is a subtype of some type named in that clause.

  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](9.9.md)).

- Otherwise, the method invocation is *ambiguous*, and a compile-time error occurs.
