1

When I run this program why it is calling method overloaded with string parameter.

public class StaticBindTest {

    public static void main(String args[])  {
       StaticBindTest et = new StaticBindTest();
       et.sort(null);

    }

    //overloaded method
    public void sort(Object c){
        System.out.println("Inside Collection sort method");
    }


   //another overloaded method
    public void sort(String hs){
        System.out.println("Inside HashSet sort method");
    }

}

In case if I re-write my method as

    public void sort(String hs){
        if(hs instanceof String)
        System.out.println("Inside HashSet sort method");
    }

It will display blank console, which means it is not a instance of String then why it call in this manner?

Prashant
  • 692
  • 2
  • 11
  • 26

3 Answers3

3

t's because In case of method Overloading

The most specific method is choosen at compile time.

As 'java.lang.String' is a more specific type than 'java.lang.Object'. In your case the method which takes 'String' as a parameter is choosen.

Clearly mentioned in DOCS

Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
  • In case if I declare another overloaded method with Integer type that it is giving me ambiguity error at compile time only. Why is it so. – Prashant Apr 24 '13 at 07:52
  • 1
    @HyperLink because then it *is* ambiguous which one is the "most specific". At the moment, `String` is a subclass of `Object` so out of the two it's clearly more specific. But if you add `Integer` into the mix, is that more or less specific than `String`? They're not in the same hierarchy so it's impossible to say. And so Java cannot determine which is the most specific method. – Andrzej Doyle Apr 24 '13 at 07:57
  • how can i call the least specific method ?? – Sagar Nayak Jun 30 '17 at 10:10
1

When I execute your code, I found Inside HashSet sort method is in output. null is empty reference to object. Both methods have Object as input that is why the method with more narrow hierarchy String is called.
Do not write StaticBindTest et = new StaticBindTest();
Just call sort(null) without object et.

Freak
  • 6,786
  • 5
  • 36
  • 54
user1379574
  • 689
  • 4
  • 11
  • 23
0

The full methodology is explained in section 15.12.2 of the JLS, and in particular 15.12.2.5 (choosing the most specific).

The summary is that Java always chooses the most specific overload that is applicable. Since null can be an instance of any class, both methods are candidates. And since String is a subclass of Object, it is definitely more specific. Hence the String overload is called.

As for your closing paragraph, null is not an instanceof anything. If you had written your Object-overload method in a similar way, it also would not have printed anything - so this is not strong evidence as to whether Java should have chosen one overload or another.

Andrzej Doyle
  • 102,507
  • 33
  • 189
  • 228
  • In case if I declare another overloaded method with Integer type that it is giving me ambiguity error at compile time only. Why is it so. – Prashant Apr 24 '13 at 07:55