In
java
the static method of the type var-args are chosen last
The following principles follow here
Widening beats var-args
Var-args are given least preference for method calling
i.e. the methods with var-arg type are not chosen if there are parameters those are wider version of any
argument we are passing
here in case when you are calling the method overloading
when you call the method by passing the String array to it as
now that we are passing the array type
there are two methods available
first is var-arg and another is wider version
here
and Array is an object in java
hence the method with wider parameter gets selected
hence the method overloading(Object... o); gets called
you may wonder here as the method called is also an var-arg method
but please notice once again
there scope for the argument aa in overloading(aa) to widen to Object type
even if you change the code to
then the output does not change
Now when you change it to
and call the method using
then again there are two options
now here
both are array var-args
first is the String[] and the second is the Object[]
and we are passing the String[] to the method
Now the question is why the method with Object[] is not selected
notice here that
when you call method using overloading(aa)
there is parameter available i.e. String[]...
A var-arg parameter can take any number of arguments , even 0
so there is exact match
(Object[]... o) is not the exact match because there was widening from String to Object
Hope this is clear to now
