I am passing a null value as a parameter to a method the code is like that--
[
public class A
{
public void ob1(Object o)
{
sopln("object version");
}

public void ob1(String s1)
{
sopln("String version");
}

public static void main(string args[])
{
A a=new A();
a.ob1(null);

}
}]
we r getting the value "string version" y it is not printing "object version"? Both r objects but how the compiler treats it?
plz help me in solving this question

peter_budo commented: I asked you to use code tags while inserting code in the post. Please do so in the future. Don't be ignorant... -1

Recommended Answers

All 6 Replies

Java will always take the most specific applicable method. In this case, using null, the String one is the most specific, applicable method.

Java will always take the most specific applicable method. In this case, using null, the String one is the most specific, applicable method.

Seems like String one is the ONLY applicable method.
At the call options are create and Object using "null" or create a String using "null".. Object doesn't have a constructor that takes argument (only one c'tor that takes no args) whereas String does.. So only one method applicable and that is public void ob1(String s1)

Seems like String one is the ONLY applicable method.
At the call options are create and Object using "null" or create a String using "null".. Object doesn't have a constructor that takes argument (only one c'tor that takes no args) whereas String does.. So only one method applicable and that is public void ob1(String s1)

What? That sopln("whatever") is not a constructer, it is simply an abbreviation for System.out.println. And the other two public void ob1(Whatever w) definitions are methods, not constructors.

The only constructor involved anywhere in that code is on the line A a = new A(); .

So, as you can see, both methods where applicable, but the String is most applicable.

What? That sopln("whatever") is not a constructer, it is simply an abbreviation for System.out.println. And the other two public void ob1(Whatever w) definitions are methods, not constructors.

Of course !

The only constructor involved anywhere in that code is on the line A a = new A(); .
So, as you can see, both methods where applicable, but the String is most applicable.

Well, I assumed that while compiling a.ob1(null); compiler would try to do an implicit conversion from null to String and Object by interpreting the code something like this: a.ob1( new String( null ) ) OR a.ob1( new Object( null ) ) . Then given that Object doesn't have a constructor that takes an arg, it would clear that the call should be resolved to ob1(String s) and not ob1(Object o).
Interestingly when I compiled this code:

package kash;

public class Test {
    
    public Test(){
        sop("Test()") ;
    }

    public static void sop(String s) { System.out.println(s); }
    
    /*public static void ob1(Object o) {
        sop("object version");
    }*/

    public static void ob1(String s1) {
        sop("String version");
    }
    
    public static void ob1(Test t) {
        sop("Test version") ;
    }
    
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        ob1(null) ;
    }
}

it said:
Test.java(25): The method ob1(String) is ambiguous for the type Test.

which means that the assumption was wrong.

In the case of OP's code, the overloaded method took arguments of type Object and String which is a subclass of Object i.e. a specialized object. Hence the String version of the code is called.

In case of Kashyap's code, the overloaded method took arguments of type String and Test, both of which were subclasses or Object i.e. specialized objects and hence the overloaded method was declared to be ambiguous.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.