Can any one explain?
When we are overloading a constructor with different parameters one having data type object and other having data type string, and when we are creating the object of this class with providing input parameter as null it is calling the constructor with string as input parameter but not the constructor having input parameter as Object. Since Object is the super class of String, can any one tell me why it is calling constructor with input parameter string?
Class A
{
public A(Object o)
{
System.out.println("Object Drawn");
}
public A (String o)
{
System.out.println("String Drawn");
}
public static void main(String args[])
{
new A(null);
}
}
Output:- String Drawn
It always calls the most specific matching method or constructor. If it didn't you would always call Object and overloading it would be pointless.
This approach is using in Java and C++
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With