Unexpected overloaded method compiler selection with null parameter [duplicate] - java

This question already has answers here:
How to do method overloading for null argument?
(7 answers)
Closed 6 years ago.
I am very surprised why output is very much different from what I am expecting , I have two overloaded methods, one having one String and the other an Object as parameter, while calling this method with null parameter, output is just printing
"String"
and not calling method having object as parameter.
Why does Java selects the method having String as parameter, how java determines which overloaded method to call ?
class TestingClass {
public void test(String s) {
System.out.println("String");
}
public void test(Object o) {
System.out.println("Object");
}
public static void main(String[] args) {
TestingClass q = new TestingClass();
q.test(null);
}
}

There is no overriding in this code, only method overloading. When the compiler has to choose which of the two test methods to execute (since null can be passed to both), it chooses the one with the more specific argument type - test(String s) - String is more specific than Object since it is a sub-class of Object.
The other method can be called using :
q.test((Object) null);

Without adding a possible new answer, let me suggest you to extend your code like following,
public class Overload {
public static void main(String[] args) {
new Overload().test(null);
}
public void test(String s) {
System.out.println("String");
}
public void test(Object o) {
System.out.println("Object");
}
public void test(Integer s) {
System.out.println("Integer");
}
}
Comment out any one of Object or Integer version any see it for yourself. The answer being already provided by others.

Related

tricky method Overloading in java [duplicate]

This question already has an answer here:
Method overloading/overriding in Java subclass object with superclass reference variable
(1 answer)
Closed 6 years ago.
class ABC {
void doMe(String s) {
System.out.println("String");
}
}
class XYZ extends ABC {
void doMe(Object o) {
System.out.println("Object");
}
}
public class StopStart {
public static void main(String[] args){
ABC o = new XYZ();
o.doMe(null);
}
}
What will happen and why?
string class extends Object. So which doMe() will execute.
Is it a compilation error?
Overloading is resolved in compile time, according to the compile time type of the variable o. Therefore doesn't matter which methods are defined in the XYZ sub-class. Only the methods of ABC are relevant.
Therefore only void doMe(String s) is considered by the compiler, and that's the chosen method.

what is use of this::instanceMethod use as method parameter in java8 [duplicate]

This question already has answers here:
:: (double colon) operator in Java 8
(17 answers)
Closed 7 years ago.
I just started learning java8 stream api. I have seen a method which has input type Runnable interface even it's allow to pass this::greet as param. when program is run, why does this not call greet method? its output only Hello, world!2. why does it allow to pass such method even input is runnable interface?
public class TestAnno {
public static void main(String[] args) {
TestAnno anno=new TestAnno();
}
public TestAnno() {
display(this::greet); // what is use of calling this method
display(this.greet()); //compiler error: The method display(Runnable) in the type TestAnno is not applicable for the arguments (void)
}
private void display(Runnable s) {
System.out.println("Hello, world!2");
//Arrays.sort(new String[]{"1","2"}, String::compareToIgnoreCase);
}
public void greet() {
System.out.println("Hello, world! greet");
}
}
I have created interface to understand it.
public interface Inter1 {
void hello();
void hello1(int a);
}
Now I change display method param to Inter1 instead of Runnable. it throw error 'The target type of this expression must be a functional interface'.
like
public class TestAnno {
public static void main(String[] args) {
TestAnno anno=new TestAnno();
}
public TestAnno() {
display(this::greet); // The method display(Inter1) in the type TestAnno is not applicable for the arguments (this::greet)
display(()->this.greet());//The target type of this expression must be a functional interface
}
/*private void display(Runnable s) {
System.out.println("Hello, world!2");
Arrays.sort(new String[]{"1","2"}, String::compareToIgnoreCase);
}*/
private void display(Inter1 s){
}
public void greet() {
System.out.println("Hello, world! greet");
}
}
Can any one help on this!!
this::greet refers to a method with no parameters and no return value. As such, it matches the single method of the Runnable interface - void run().
Your display method accepts a Runnable instance. It must execute that's Runnable's run method in order for your greet method to be executed :
private void display(Runnable s) {
s.run();
System.out.println("Hello, world!2");
}
Your second attempt to call display can pass compilation if you turn it into a lambda expression :
display(()->this.greet());

Why can null be passed to an overloaded Java method? [duplicate]

This question already has answers here:
How to do method overloading for null argument?
(7 answers)
Closed 7 years ago.
I found this Java code from this site. I don't understand how it compiles without ambiguous error.
package swain.test;
public class Test {
public static void JavaTest(Object obj) {
System.out.println("Object");
}
public static void JavaTest(String arg) {
System.out.println("String");
}
public static void main(String[] args) {
JavaTest(null);
}
}
Output:
String
null can be passed to both JavaTest(String arg) and JavaTest(Object obj), so the compiler chooses the method with the more specific argument types. Since String is more specific than Object (String being a sub-class of Object), JavaTest(String arg) is chosen.
If instead of JavaTest(Object obj) you had JavaTest(Integer obj), the compilation would have failed, since Integer is not more specific than String and String is not more specific thanInteger`, so the compiler wouldn't be able to make a choice.

Couldn’t understand the output of this code? [duplicate]

This question already has answers here:
Strange Java null behavior in Method Overloading [duplicate]
(4 answers)
Closed 9 years ago.
public class Test {
public void method(String param)
{
System.out.println("String version");
}
public void method(StringBuffer param)
{
System.out.println("String Buffer");
}
public static void main(String args[])
{
Test test=new Test();
test.method(null);
}
}
This code result is compilation error says “reference to method is ambiguous”
public class Test
{
public void method1(Object param)
{
System.out.println("Object Version ");
}
public void method1(String param)
{
System.out.println("String Version ");
}
public static void main(String[] args)
{
Test test=new Test();
test.method1(null);
}
}
This code result is “String Version”
Actually I can’t understand the result of second piece of code. Why don’t both piece of code has the same result
In first case,
null is a subtype of every other reference type.
So, compiler finds ambiguity in deciding which method to call..
In second case, it finds more specific object for null which happens to be String. Hence it calls method1 and prints String Version

What is the explanation of this java code?

I have the following code :
public class Main {
public void method(Object o)
{
System.out.println("Object Version");
}
public void method(String s)
{
System.out.println("String Version");
}
public static void main(String args[])
{
Main question = new Main();
question.method(null);//1
}
}
why is the result is "String Version" ? and why there is a compiler error if the first method takes a StringBuffer object ?
Another case : if the first method takes a StringBuffer object and I write question.method("word");the result will be "String Version" . Why ? why there is no compiler error ?
The JAVA spec says that in cases like this, the most specific function will be called. Since String is a sub type of Object - the second method will be called.
If you change Object to StringBuffer - there is no specific method since StringBuffer is not a sub type of String and vice versa. In this case the compiler does not know which method to call - hence the error.
When looking at the other case :
package com.snamellit;
public class Main {
public void method(Object o) {
System.out.println("Object Version");
}
public void method(String s) {
System.out.println("String Version");
}
public static void main(String args[]) {
Main question = new Main();
question.method("word");
}
}
If the first method tqkes a StringBuffer and the second a String, there is no confusion possible as "word" is a String and not a StringBuffer.
In Java the identity of a function/method is dependent on 3 things : the name, the type pf the parameters (aka the argument signature) and the classloader. Since both types have a different argument signature the compiler can easily choose the right one and does not raise an error.

Categories