1
public class NullDemo {

/**
 * @param args
 */
public static void main(String[] args) {
    // TODO Auto-generated method stub
    NullDemo n = new NullDemo();
    n.execute(null);

}

public void execute(Object o) {
    System.out.println("object");
}

public void execute(Double o) {
    System.out.println("double");
}
 }

i have executed this above code and it execute the method with execute(Double o).i need to know the reason why it executed execute(Double o) and not execute(Object o)

and suppose

 public class NullDemo {

/**
 * @param args
 */
public static void main(String[] args) {
    // TODO Auto-generated method stub
    NullDemo n = new NullDemo();
    n.method1(null); /// give the compilation error

}

public void method1(Float o) {
    System.out.println("object");
}

public void method1(Double o) {
    System.out.println("double");
}
}

if i make the method public void method1(Float o) and public void method1(Double o) it will give the compilation error why this is so? is this related with hierarchy?

T.S.
  • 18,195
  • 11
  • 58
  • 78
ZohebSiddiqui
  • 209
  • 3
  • 4
  • 14
  • I think the compilation error is issued because it has a hard time distinguishing between double and float - so it is ambiguous as to which function to choose. – mdebeus Jun 21 '14 at 05:18
  • 2
    You can find the answer here: http://stackoverflow.com/questions/19243708/how-do-overloaded-methods-work/19243758#19243758 – Abdulrazak Alkl Jun 21 '14 at 05:25
  • 1
    And now I realize it didn't have Java as the original tag. Sad times. – Makoto Jun 21 '14 at 06:30

3 Answers3

1

Java will prefer to call the method which is furthest down the inheritance tree. Consider the following situation:

class Demo {

    public void doSomething(ParentClass foo) {
        //.....
    }

    public void doSomething(ChildClass foo) {
        //.....
    }

    public static void main() {
        Demo demo = new Demo();
        demo.doSomething(new ChildClass());
    }    
}

In the above situation, the method call can match either of the two methods. However, you can see intuitively that it should match the second method.

You can read about the exact behavior in the java spec: http://docs.oracle.com/javase/specs/jls/se8/html/jls-15.html#jls-15.12.2

Sam Dufel
  • 17,560
  • 3
  • 48
  • 51
0

Given your initial code, adding a cast to (Object) will print what you expect. This happens because Double is more specific then Object and it is thus called by default,

public static void main(String[] args) {
  NullDemo n = new NullDemo();
  n.execute((Object) null);
}

public void execute(Object o) {
  System.out.println("object");
}

public void execute(Double o) {
  System.out.println("double");
}
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
0

If two or more methods could be applicable to a method call, the compiler will choose the one which is more specific, if possible. In the first example, the method execute(Double o) is more specific since Double is a subclass of Object. In the second example, neither method is more specific, since neither Double nor Float is a subclass of the other. Since both methods could be applicable (null would work as either a Double or a Float), and neither one is more specific, the compiler reports that the call is ambiguous.

ajb
  • 31,309
  • 3
  • 58
  • 84