MakeMyTrip interview question

void sort(String s){ } void sort(Integer I){ } void sort(Object o){ } calling : sort(null) which function will be called?

Interview Answers

Anonymous

12 July 2016

Void Sort(String s) will be called , the reason is in java at run time method overloading goes with the most specific case and as we know null is an instance of string having length as 0.

1

Anonymous

27 Oct 2018

It will show compiler error as it will be confused to go with Integer or String. But if have only Integer, or String or Object or String and Object both or Integer and Object both, it will compile successfully and will go to Integer or String method resp.

Anonymous

20 Aug 2016

It'll fail with compile error. Its an ambiguous method call. because, both sort(String) and sort(Integer) will match 'null'. FYI, null is NOT an instance of String.