Constructor Overloading in Java

Constructor are used to create the objects . new keyword always calls the  constructor of the class.

 Constructor name is always be the same as the name of the class .Generally in 99% code ,the name of the method is  different from the name of the class . But the problem arises  that method  too can have same name as the name of the class ( considered as bad coding practice)

Constructor example :

public class JavaBuff          // Class name                  
    {                                                                     
     public JavaBuff()            // Default Constructor   
       { } 
    public void JavaBuff()   //Valid but considered bad coding practice
      {}
} 

 If the name is same , then the only way we can differentiate between constructor and method , by looking at the return type .  Constructor never has a return type while method always has a return type .

               

 Points to ponder :

1.   While creating new object for the class ,every constructor calls the constructor of the superclass first , which is also known as constructor chaining . Internally , an implicit call to super() has been made ,unless the constructor invokes an overloaded constructor of the same class .

2.  Constructor can use any access modifier . Keep in mind that constructor can use private also .

3.  Constructor name should be same as the name of the class .

4.  Unlike methods , constructor do not have return types  .

5.  If one do not provide any constructor for the class ,then compiler put one implicitly in the code.
   
6. The default constructor is always no argument constructor .

7. If you have typed your constructor then , compiler do not provide the implicit constructor . So if you have overloaded the constructor , and also want to use the no arg constructor , then you need to write it by yourself in the code .

8. One cannot make a call to an instance method, or access an instance variable,
until after the super constructor runs.

9. Abstract classes always has constructor , and those constructors are called when a concrete (the class which implemented the abstract class) subclass is instantiated (creating an object).

10. Another important point is that interfaces do not have constructors .

11 . The first line in the constructor must be either call to super() or call to this() .Calls to this() and super() cannot be in the same constructor. You can have one or the other, but never both.

12. Every class, even an abstract class, has at least one constructor

13. Constructors are never inherited, thus they cannot be overridden.

Overloading a Constructor

Just like methods constructors can also be overloaded .Overloading a constructor means writing a multiple version of the constructor .

Continuing with  above example

public class JavaBuff          // Class name                
{                                                                    
    public JavaBuff()            //Default Constructor 1  
      { }  
    public  JavaBuff(int a , int b)   // Overloaded Constructor 2  
      { }
    public  JavaBuff(String x , String y)   // Overloaded Constructor 3  
      { }
 }