Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>because it is <strong>ambiguous</strong>.</p> <p><code>2</code> can be <code>Integer</code> as well as <code>long</code> and it can be resolve to both. you made compiler confused whom to invoke :)</p> <hr> <h2>5.12.2.2 Choose the Most Specific Method</h2> <blockquote> <p>If more than one method declaration 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 declaration 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 type error.</p> <p>The precise definition is as follows. Let m be a name and suppose that there are two declarations of methods named m, each having n parameters. Suppose that one declaration appears within a class or interface T and that the types of the parameters are T1, . . . , Tn; suppose moreover that the other declaration appears within a class or interface U and that the types of the parameters are U1, . . . , Un. Then the method m declared in T is more specific than the method m declared in U if and only if both of the following are true:</p> <p>T can be converted to U by method invocation conversion. Tj can be converted to Uj by method invocation conversion, for all j from 1 to n. A method is said to be maximally specific for a method invocation if it is applicable and accessible and there is no other applicable and accessible method that is more specific. If there is exactly one maximally specific method, then it is in fact the most specific method; it is necessarily more specific than any other method that is applicable and accessible. It is then subjected to some further compile-time checks as described in §15.12.3.</p> <p>It is possible that no method is the most specific, because there are two or more maximally specific methods. In this case:</p> <p>If all the maximally specific methods have the same signature, then: If one of the maximally specific methods is not declared abstract, it is the most specific method. Otherwise, all the maximally specific methods are necessarily declared abstract. The most specific method is chosen arbitrarily among the maximally specific methods. However, the most specific method is considered to throw a checked exception if and only if that exception is declared in the throws clauses of each of the maximally specific methods. Otherwise, we say that the method invocation is ambiguous, and a compile-time error occurs.</p> </blockquote> <p><strong>15.12.2.3 Example: Overloading Ambiguity</strong></p> <pre><code>Consider the example: class Point { int x, y; } class ColoredPoint extends Point { int color; } class Test { static void test(ColoredPoint p, Point q) { System.out.println("(ColoredPoint, Point)"); } static void test(Point p, ColoredPoint q) { System.out.println("(Point, ColoredPoint)"); } public static void main(String[] args) { ColoredPoint cp = new ColoredPoint(); test(cp, cp); // compile-time error } } </code></pre> <p>This example produces an error at compile time. The problem is that there are two declarations of test that are applicable and accessible, and neither is more specific than the other. Therefore, the method invocation is ambiguous. If a third definition of test were added:</p> <pre><code>static void test(ColoredPoint p, ColoredPoint q) { System.out.println("(ColoredPoint, ColoredPoint)"); } </code></pre> <p>then it would be more specific than the other two, and the method invocation would no longer be ambiguous.</p> <ul> <li><a href="http://java.sun.com/docs/books/jls/second_edition/html/expressions.doc.html" rel="nofollow">Read More</a></li> </ul>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload