I am confused with the Output of the following two programs.
When I'm only having two methods with parameters as String
and Object
in Program 1 it gives me output as String
.
But when I add a new method with parameter as Integer
in Program 2 it won't compile and gives error as
The method nullTest(Object) is ambiguous for the type testNull
Program 1 :
package onkartest;
public class TestNull {
public static void nullTest(Object b)
{
System.out.println("object");
}
public static void nullTest(String x)
{
System.out.println("String");
}
public static void main(String x[])
{
nullTest(null);
}
}
Output : String
Program 2 :
package onkartest;
public class TestNull {
public static void nullTest(Object b)
{
System.out.println("object");
}
public static void nullTest(String x)
{
System.out.println("String");
}
public static void nullTest(Integer i)
{
System.out.println("Integer ");
}
public static void main(String x[])
{
nullTest(null);
}
}
Output :
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The method nullTest(Object) is ambiguous for the type testNull
at onkartest.testNull.main(testNull.java:26)
And also if I run the program with keeping only Object
parameter method, it gives me output as Object
.
Can you explain me the reason behind this behavior?