"gk" <srcjnu@gmail.com> wrote in message
news:1158242237.257265.133360@e3g2000cwe.googlegroups.com...
public class PowerSupply
{
public PowerSupply(String voltage){
System.out.println ("PowerSupply(String) executed");
}
public PowerSupply(Object voltage){
System.out.println ("PowerSupply(Object) executed");
}
public PowerSupply(){
System.out.println ("No argument constructor execute");
}
public static void main(String[] args) {
PowerSupply ps = new PowerSupply(null);
}
}
what would be the output ?
output: PowerSupply(String) executed > >
i dont know why the JVM is choosing the first constructor for
argument null .
please explain...how this output coming ?
JLS states that the when selecting between overloaded methods, the "most
specific" method will always be chosen. They then go into great lengths
defining "most specific". Informally, any parameter that could be passed
into the first constructor (String voltage) could also be passed into the
second constructor (Object voltage), but not the other way around, so the
first constructor is "more specific" than the second one.
- Oliver