Forums Register Login

why not method(Object)

+Pie Number of slices to send: Send
Hi, Can any body please explain me why method(Integer) is called for method(null)?
public class A{
public static void main(String... arg){
A returnType = new A();
A.method(null);
}
public void method(Object obj){
System.out.println("Object is called");
}
public void method(Integer longvalue){
System.out.println("Integer is called");
}
}

output:-
Integer is called.
+Pie Number of slices to send: Send
Search around for the term "most specific". This question has been asked and answered a couple of times before.
+Pie Number of slices to send: Send
Please UseCodeTags. You made a mistake when copying it: A.method(null); should be returnType.method(null);.

That is because Java always called the most specialized method. And because Integer is more specialized then Object (Integer extends Object) the Integer function is called. For fun add a function with a Double argument.
We find this kind of rampant individuality very disturbing. But not this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 645 times.
Similar Threads
(MyClass) null
Question Regarding Boxing
null passed as parameter
AutoBoxing
null as arg in mehods
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 16, 2024 00:10:24.