This question already has answers here:
How is an overloaded method chosen when a parameter is the literal null value?
(8 answers)
Closed 6 years ago.
Which overloaded method will be called for the below method and why?
I executed this code and it calls the overloaded method with List but why does it happen?
public class AmbigiousOverload {
public static void add(Object o) {
System.out.println("Overloaded method with Object.");
}
public static void add(List l) {
System.out.println("Overloaded method with List.");
}
public static void main(String[] args) {
add(null);
}
}
Output: Overloaded method with List.
The List overload is called because the List overload is the most specific matching overload, as all List implementations are subclasses of Object.
From JLS Sec 15.12.2.5:
The informal intuition is that one method is more specific than another if any invocation handled by the first method could be passed on to the other one without a compile-time error.
Since any parameter you can pass to add(List) can also be passed to add(Object), add(List) is more specific. As such, given that null can be passed to both, the more specific method is the one chosen.
Note that it wouldn't compile if you had an add(String) overload, since List and String are "equally" specific. (Or any other class: it doesn't have to be String).
Related
This question already has answers here:
How does Java choose which constructor to use?
(2 answers)
Closed 6 years ago.
I have a Java class where there are two parameterized constructors
public class TestApplication {
TestApplication(Object o)
{
System.out.println("Object");
}
TestApplication(double[] darray)
{
System.out.println("Array");
}
public static void main(String[] args)
{
new TestApplication(null);
}
}
When I run this program I get output as "Array". Why does the Object constructor not run?
Constructor overloading resolution behaves the same as method overloading resolution. When two constructors match the passed parameter, the one with the more specific argument types is chosen.
null can be assigned to both a double[] and an Object, since both are reference types, but an array is a more specific type than Object (since arrays are sub-classes of the Object class), so new TestApplication(null) triggers the TestApplication(double[] darray) constructor.
This question already has answers here:
How is an overloaded method chosen when a parameter is the literal null value?
(8 answers)
Closed 7 years ago.
I have the following structure of my class:
void add(String s){
System.out.println("string");
}
void add(Object s){
System.out.println("object");
}
public static void main(String[] args) {
new MyClazz().add(null);
}
O/P : string
Why object is not called?
String is more specific than Object. Therefore void add(String s) is preferred over void add(Object s).
15.12.2. Compile-Time Step 2: Determine Method Signature
The second step searches the type determined in the previous step for
member methods. This step uses the name of the method and the argument
expressions to locate methods that are both accessible and applicable,
that is, declarations that can be correctly invoked on the given
arguments.
There may be more than one such method, in which case the most
specific one is chosen. The descriptor (signature plus return type) of
the most specific method is the one used at run time to perform the
method dispatch.
This question already has answers here:
Method overloading and choosing the most specific type
(9 answers)
Closed 8 years ago.
class Test {
public Test(Object obj) {
System.out.println("Object");
}
public Test(String s) {
System.out.println("String");
}
public static void main(String[] args) {
new Test(null); //prints String. Why not Object?
}
}
If I add another constructor with argument of type Integer ,or, for that matter any other type, calling new Test(null); results in compilation error - The constructor Test(Object) is ambiguous. Why no error is generated for the above example? On executing it, constructor with argument String is called. Why constructor with argument type Object is not called? How this ambiguity is resolved?
//prints String. Why not Object?
Because compiler choose most specific type.
If I add another constructor with argument of type Integer ,or, for
that matter any other type, calling new Test(null); results in
compilation error - The constructor Test(Object) is ambiguous.
Now String and Integer are in the same level in the object hierarchy, So, compiler can't choose one out of those two
Because it is determined by the most specific type of the parameter.
Since String is subclass of Object, and null is subtype of anything, then the second constructor is called, because String is more specific than Object.
Compiler is designed to pick up the overloaded method that very closely matches the Value sent in parameter.
This question already has answers here:
Java method call overloading logic
(3 answers)
Closed 9 years ago.
If java is pass-by-value, and that value is the memory address for the actual type, then why does the overloaded method that's called get decided by the reference/declared type?
class Boss {
void test(Object o){
System.out.println("object");
}
void test(Boss b){
System.out.println("boss");
}
public static void main( String[] args ) {
Boss b = new Boss();
b.test((Object)b); //prints out object, why?
}
}
Dynamic binding applies to the object on which the method is called, not to its parameters and method overloading.
In this case, the method is determined at compile time to be void test(Object o), because that's the overload that matches the argument types.
At run time, the implementation of test(Object o) is chosen based on the object on which it's called. In this case, that's the implementation in Boss.
As an example, say you had had done this:
class Director extends Boss { ... }.
Boss d = new Director();
Boss b = new Boss;
d.test((Object)b);
Then in terms of method overloading, the method chosen at compile time would still be test(Object o). At runtime, the implementation could be in Director, because d references a Director.
Overloading is decided at the compile time i.e the compiler decides which method to execute right during compilation, irrespective of the object that would be passed to it at runtime. And since you are passing a reference variable of Object and not Boss, although the object is of Boss, it executes the overloaded void test(Object b) method.
This question already has answers here:
Java method dispatch with null argument
(4 answers)
Understanding which constructor is chosen and why
(4 answers)
Closed 9 years ago.
When I run this code it prints String. My question is why there is no compile time error?
Default value of Object and as well as String is null. Then why not compiler says Reference to method1 is ambiguous.
public class Test11
{
public static void method1(Object obj) {
System.out.println("Object");
}
public static void method1(String str) {
System.out.println("String");
}
public static void main(String[] arr ) {
method1(null);
}
}
From this answer:
There, you will notice that this is a compile-time task. The JLS says in subsection 15.12.2:
This step uses the name of the method and the types of the argument expressions to locate methods that are both accessible and applicable There may be more than one such method, in which case the most specific one is chosen.
The compiler will look at all the possible overloads of the method that could match the parameters you pass. If one of them is more specific than all the others then it will be chosen, it's only considered ambiguous if there is no single most specific overload.
In your example there are two possible overloads, method1(Object) and method1(String). Since String is more specific than Object there's no ambiguity, and the String option will be chosen. If there were a third overload such as method1(Integer) then there is no longer a single most specific choice for the call method1(null), and the compiler would generate an error.
Well in one sentence
In case of Overloaded methods compiler chooses the method with most
specific type, as String is the most specific type of Object compiler
will invoke the method which takes string as an argument
public class Test11
{
public static void method1(String str)//here str is the object of string
{
System.out.println("String");
}
public static void method1(Object obj)//here this is a common object not specified
{
System.out.println("Object");
}
public static void main(String[] arr )
{
Test11 t=new Test11();
String null1=new String();
method1(null1);
method1(t);
}
}
output is :
String
Object
//null1- is a string object if u pass this it will call method1(String str) only because u pass string object
//t-is general object if u pass this it will call method1(Object obj)only because u pass class objct so it will pass object as parameter