简体   繁体   中英

Constructors and Constructors Overloading

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++

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM