+ 15

Why it is working like this ?

static void m(Object o){ S.o.p("object")} static void m(String s){ S.o.p("string")} p s v main(String [] args){ m(null) } O/p : string Why not object, even object is null by default ?

18th Feb 2020, 11:16 AM
Ozair Khan
Ozair Khan - avatar
7 Answers
+ 2
See it is because , while resolving overloaded methods compiler will always give precedence to child type argument than compared with parent type argument
18th Feb 2020, 12:18 PM
Satyam Yadav
Satyam Yadav - avatar
+ 8
If the method with String parameter is removed the o/p is "object".
18th Feb 2020, 11:53 AM
Ozair Khan
Ozair Khan - avatar
+ 5
null is not a object here it's a value because it gives null pointer exception if you do like this null.equals("abc"). That's why the o/p is string.
18th Feb 2020, 11:31 AM
A͢J
A͢J - avatar
+ 4
Ozair Khan Because there is only one method with object parameter and object can also be null. When you were using two methods with different parameter then method with String parameter was overloading to method with Object parameter so that time output was string.
18th Feb 2020, 12:24 PM
A͢J
A͢J - avatar
+ 4
While resolving overloaded method compiler will always give the precedence for child type argument when compared with parent type argument.
6th Mar 2020, 3:08 AM
Ketul Patel
Ketul Patel - avatar
+ 3
Reference objects, such as Strings might have null as their default values.
18th Feb 2020, 11:20 AM
Seb TheS
Seb TheS - avatar
+ 2
Maybe you can get the same result using arrays.
18th Feb 2020, 11:29 AM
Seb TheS
Seb TheS - avatar