Why is the output like this?

Posted by javatechi on Stack Overflow See other posts from Stack Overflow or by javatechi
Published on 2010-06-14T12:48:15Z Indexed on 2010/06/14 13:22 UTC
Read the original article Hit count: 152

Filed under:
|
class another {
    public void method(Object o) {
        System.out.println("This is in method which takes object");
    }
    public void method(String s) {
        System.out.println("This is method which takes string");
    }
}

public class NewClass {
    public static void main(String args[]) {
        another an = new another();
        an.method(null);
    }
}

When I try to execute this, I get

This is method which takes string

as the output. Why not "This is in method which takes object"? Object can also be null and string can also be null, why doesn't it invoke first method?

© Stack Overflow or respective owner

Related posts about java

Related posts about methods