Constructor:Overloading ,Examples ,Basics, Rules,Important Points

Constructor is one of the most common term in java . Yet it is so big in details and meaning that we need to write an entirely new post for it .

What is Constructor ?

Constructor are used to create the objects . new keyword always call constructor of the class in the java code .

Constructor and method have so much common , that it is difficult to differentiate between two . So you can easily identify constructor in the code by keeping following points in the mind

1 .  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 (Although it is considered as bad coding practice)

            Constructor example 

            JavaHungry.java

            public class JavaHungry          // Class name                  
            {                                                                    
               public JavaHungry()            // Default Constructor   
                 { } 
     

               public void JavaHungry()   //Valid Method name but it is considered as bad coding practice
                 {}
             }

2 .  So 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 . In the above example we have shown the return type in red color .  Return type is the declaration of the data type of the value which is returned to the calling method .

                 
 Points to remember :

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


    JavaHungry.java

            public class JavaHungry          // Class name                  
            {                                                                    
               public JavaHungry()            //Default Constructor 1   
                 { }  

               public  JavaHungry(int a , int b)   // Overloaded Constructor 2   
                 { }


               public  JavaHungry(String x , String y)   // Overloaded Constructor 3   
                 { }

             }




About The Author

Subham Mittal has worked in Oracle for 3 years .
For more java articles ,Click here to Subscribe JavaHungry