1

I'm unable to understand how does java "Choosing the Most Specific Method" rule in function overloading works.

I have a class where function overloading is achieved. There are two functions with the same name "show". Once accepts Object type argument and other accepts String type.

I'm calling the function passing null. The method with String type argument gets called.

class Test
{

    public void show(String s)
    {
        System.out.println("hi");
    }

    public void show(Object o)
    {
        System.out.println("hello");
    }


    public static void main(String s[])
    {
        Test t = new Test();
        t.show(null);
    }
}

The output will be "Hi". Please help me understand the explanation.

Conffusion
  • 4,335
  • 2
  • 16
  • 28
ASH
  • 101
  • 1
  • 4
  • 13
  • 3
    Are you asking why `String` is considered more specific than `Object`? – Eran Jul 30 '19 at 11:45
  • 1
    General advise: Don't overload methods, use method names that tell what is happening: `showString(String)`, `showObject(Object)`. –  Jul 30 '19 at 11:46
  • 1
    Add another method that accepts Integer, check the results – deathangel908 Jul 30 '19 at 11:46
  • My read of the JLS (15.12.2.5. Choosing the Most Specific Method) says that String is chosen over Object because String is a subtype of Object. –  Jul 30 '19 at 11:56
  • please refer this link it may help. [Method overloading and choosing the most specific type](https://stackoverflow.com/questions/9361639/method-overloading-and-choosing-the-most-specific-type) – Mohd Aarif Jul 30 '19 at 12:04
  • Please Refer this link, it may help [https://stackoverflow.com/questions/9361639/method-overloading-and-choosing-the-most-specific-type](https://stackoverflow.com/questions/9361639/method-overloading-and-choosing-the-most-specific-type) – Mohd Aarif Jul 30 '19 at 12:07
  • @LutzHorn That's a bad advise for 2 reasons: a) It leads to convoluted class signatures. b) It creates maintenance costs, because when you refactor class names, guess what will not be refactored accordingly (depends on IDE)... – SME_Dev Jul 30 '19 at 12:09
  • @LutzHorn as a general advise, this is completely wrong, sorry. Overloading is a basic OOP feature. Sure, you have to be careful, but it's not bad or even wrong. – Janos Vinceller Jul 30 '19 at 12:11
  • Josh Bloch [advised](http://thefinestartist.com/effective-java/41): " A safe, conservative policy is never to export two overloadings with the same number of parameters." I agree with him on this. See also Effective Java. –  Jul 30 '19 at 12:13
  • 3
    Possible duplicate of [Method Overloading for null argument](https://stackoverflow.com/questions/5229809/method-overloading-for-null-argument) – Janos Vinceller Jul 30 '19 at 12:14

2 Answers2

3

Java will always try to use the most specific version of a method that is available.

The two methods

public void show(Object o) { ... }

public void show(String s) { ... }

could take null as a valid value. In this scenario the overload taking a String parameter is used, because String is more specific than Object.

If you add a third method

public void show(Integer t) { ... }

your code wouldn't compile any more because String and Integer are not related. Neither is more specific than the other and the compile is not able to decide which one to use.

jsvilling
  • 233
  • 2
  • 6
2

Please see the Java language specification. As String is a more "narrow" type than Object as it is a subtype, this method will be chosen since it is more "specific".

PalBo
  • 2,203
  • 3
  • 22
  • 43