views:

317

answers:

3

Hello,

If I write this line in Java:

JOptionPane.showInputDialog(null, "Write something");

Which method will be called?

  • showInputDialog(Component parent, Object message)
  • showInputDialog(Object message, Object initialSelectionValue)

I can test it. But in other cases similar to this, I want to know what happens.

Martijn

+16  A: 

The most specific method will be called - in this case

showInputDialog(Component parent, Object message)

This generally comes under the "Determine Method Signature" step of overload resolution in the spec (15.12.2), and in particular "Choosing the Most Specific Method".

Without getting into the details (which you can read just as well in the spec as here), the introduction gives a good summary:

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 type error.

Jon Skeet
What about calling `f(null)` when the methods `f(Integer i)` and `f(String s)` are defined? Neither seems more specific than the other.
Stephan202
+1 - I was wrong.
ChssPly76
@Stephan202: In Eclipse, I get `The method f(Integer) is ambiguous for the type ExampleClass` This was with static methods
R. Bemrose
@R. Bemrose: judging by Bill's answer, this will also hold true for non-static methods. Thanks for testing!
Stephan202
+5  A: 
ChssPly76
Tested and there is no compiler error.
JCasso
@jcasso - I see a compiler error on the "doSomething" line, saying "The method doSomething(String, Object) is ambiguous for the type "
CPerkins
@CPerkins - yes, jcasso's comment refers to my original (incorrect) response where I said that `showInputDialog()` would throw a compiler error whereas it would not.
ChssPly76
+4  A: 

In your particular case the more specific method will be called. In general, though, there are some cases where the method signature can be ambiguous. Consider the following:

public class Main {

    public static void main(String[] args) {
        Main m = new Main();
        m.testNullArgument(null);
    }

    private void testNullArgument( Object o )
    {
        System.out.println("An Object was passed...");
    }

    private void testNullArgument( Integer i )
    {
        System.out.println("An Integer was passed...");
    }

    private void testNullArgument( String s )
    {
        System.out.println("A String was passed...");
    }
}

In this case, the compiler can't decide between the method that takes an Integer and the method that takes a String. When I try to compile that, I get

reference to testNullArgument is ambiguous, both method testNullArgument(java.lang.Integer) in testnullargument.Main and method testNullArgument(java.lang.String) in testnullargument.Main match
Bill the Lizard
+1, this answers the question I posed under Jon's post.
Stephan202
That makes sense. Integer and String are both "equal" in terms of specificity while Object is the parent of both, so it's more vague. Now, if you removed the String method, the Integer one would be called.
Thomas Owens
@Thomas: That's correct. It looks like it doesn't matter how far down different branches of the inheritance tree the two ambiguous classes are either, since String directly extends Object, while Integer extends Number.
Bill the Lizard