0

Can some one please explain in more detail.

public class Car 
{
    public void disp(Object o)
    {
       System.out.println("Object called :");
    }
    public void disp(String s)
    {
       System.out.println("String called :"+s);
    }
}

Calling the main method as below

 public class CarMain {

public static void main(String[] args) {
    // TODO Auto-generated method stub
    Car car=new Car();
    car.disp(null); 
}

}

Output : String Called : null

2nd scenario if I will pass Integer instead of Object in disp(Object o) then i am getting error as "The method disp(Integer) is ambiguous for the type Car"

As per understanding Java wrapper plays the major role here, but need well explanations.

Please help.

Ganesh
  • 903
  • 1
  • 8
  • 18
  • 2
    You haven't shown a `disp` method taking an Integer. Can you be more clear? – Thiyagu Jul 16 '18 at 17:21
  • 1
    public void disp(Integer o) { System.out.println("Object called :"); } – Ganesh Jul 16 '18 at 17:24
  • 2
    Your snippet [doesn't reproduce the issue](https://ideone.com/lJCHi1). Please post an [MCVE](https://stackoverflow.com/help/mcve) – BackSlash Jul 16 '18 at 17:24
  • I am expecting detailed explanation why the above posted code select method which takes String s as parameter on passing null while calling, why not Object o method – Ganesh Jul 16 '18 at 17:27
  • 1
    Because you are providing a `String`: since you have the implementation which accepts a `String` parameter, this one gets called, because it is more specific than `Object`. If you provide an object of a different type, the method which takes the `Object` will be called. – BackSlash Jul 16 '18 at 17:28
  • Thanks @AmitBera – Ganesh Jul 16 '18 at 17:41

2 Answers2

3

Let us take both the cases one by one:

Case 1: When we have two disp overloaded function, one with parameter Object and other with parameter String:

public void disp(String s) { System.out.println("String called :"+s); }

public void disp(Object o) { System.out.println("Object called :"); }

In this case, it will refer to the most specific method. According to Java Docs JLS 15.12.2.5:

A method m1 is strictly more specific than another method m2 if and only if m1 is more specific than m2 and m2 is not more specific than m1.

So, in above case, the function accepting String is more specific then the one accepting Object, however, the opposite is not true.

Hence, in this case, when null is passed, it will go to the method with parameter String. But if you will pass (Object)null, it will go to the method with parameter Object.

car.disp(null); --> String called :null
car.disp((Object)null); --> Object called :

Case 2: When we have two disp overloaded function, one with parameter String and one with parameter Integer.

 public void disp(String s) { System.out.println("String called :"+s); }
 public void disp(Integer i) { System.out.println("Integer called :"+ i); }

In this case, as none of the method is more specific then the other and both the function can accept null value, an appropriate function can not be decided in case of null. However, if you will pass (Integer)null or (String)null, no ambiguity will be there and appropriate function can be called.

car.disp(null); --> Compile time error (The method disp(String) is ambiguous for the type Car)
car.disp((Integer)null); --> Integer called :null
car.disp((String)null); --> String called :null
Aman Chhabra
  • 3,824
  • 1
  • 23
  • 39
0

As the commenters have pointed out, you don't have a disp(Integer..) method, and if you do provide that, the method call works fine with disp(12).

As for the question of disp(null), according to the Java language spec:

Overloaded methods and constructors are resolved at compile time by picking the most specific method or constructor from those which are applicable.

Basically String is more specific than Object, so even though both apply to null, the String version of the method gets picked.

hoodakaushal
  • 1,253
  • 2
  • 16
  • 31