---
spec: JLS
title: "The Java Language Specification"
edition: "Java SE 8 Edition"
java_version: 8
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/se8/html/jls-15.html#jls-15.12.2.5
id: "15.12.2.5"
url: https://jvm.obfuscated.software/jls/se8/15.12.2.5.html
raw: https://jvm.obfuscated.software/jls/se8/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 T is not a subtype of S and one of the following is true (where U_1 ... U_k and R_1 are the parameter types and return type of the function type of the capture of S, and V_1 ... V_k and R_2 are the parameter types and return type of the function type of T):

- If `e` is an explicitly typed lambda expression ([§15.27.1](15.27.1.md)), then one of the following is true:

  - R_2 is `void`.

  - R_1 `<:` R_2.

  - R_1 and R_2 are functional interface types, and there is at least one result expression, and R_1 is more specific than R_2 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_1 is a primitive type, and R_2 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_1 is a reference type, and R_2 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.

- If `e` is an exact method reference expression ([§15.13.1](15.13.1.md)), then i) for all *i* (1 ≤ *i* ≤ *k*), U_i is the same as V_i, and ii) one of the following is true:

  - R_2 is `void`.
  - R_1 `<:` R_2.
  - R_1 is a primitive type, R_2 is a reference type, and the compile-time declaration for the method reference has a return type which is a primitive type.
  - R_1 is a reference type, R_2 is a primitive type, and the compile-time declaration for the method reference has a return type which is a reference type.

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

- If `e` is a conditional expression, then 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 applicable and accessible 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)), then:

  - If exactly one of the maximally specific methods is concrete (that is, non-`abstract` or default), it is the most specific method.

  - Otherwise, if all the maximally specific methods are `abstract` or default, and the signatures of all of the maximally specific methods have the same erasure ([§4.6](4.6.md)), then the most specific method is chosen arbitrarily among the subset of the maximally specific methods that have the most specific return type.

    In this case, the most specific method is considered to be `abstract`. Also, the most specific method is considered to throw a checked exception if and only if that exception or its erasure is declared in the `throws` clauses of each of the maximally specific methods.

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