Thursday 28 August 2014

Constructors in Java



Definition of Constructor :

A Constructor is used to initialize an object immediately upon creation. It has the same name as that of a class in which it resides and is syntactically similar to a method. 
A constructor has no return type, not even void. This is because the implicit return type of a class’ constructor is the class type itself.

For example : The constructor of a class Demo is like Demo(), so it is like a method with the same name as that of a class.

Why we need Constructors ?

Whenever we write a program, it can be tedious to initialize all of the variables in a class each time an instance is created. 
It would be simpler and more concise to have all of the setup done at the time the object is first created. 
Java allows objects to initialize themselves when they are created by the use of Constructors.

Default Constructor :

Whenever we use new keyword for the creation of an object in the program with the class name than the automatic or default constructor is called which has no parameters and it initializes all the variables of an object.

For example :

1:  class DemoConstructor  
2:  {  
3:       DemoConstructor()  
4:       {  
5:            System.out.println("This is the default constructor");  
6:       }  
7:       public static void main(String[] args)  
8:       {  
9:            // we called the default constructor with new keyword.  
10:            DemoConstructor obj = new DemoConstructor();  
11:       }  
12:  }  

Output :



Note : If the constructor is not implemented in the program than the default constructor is called automatically by Java run time environment but if we uses our own constructor in the program like above (It is also called constructor overloading) than the default constructor by java run time environment never called. 
So, constructor is optional in a class.

Parameterized constructor :

Parameterized constructor is the one which takes parameters and if we use that constructor than also the default constructor will be overloaded to the new parameterized constructor.
We can use more than one constructor at a time in a class with different signatures i.e. we can overload the constructor by different ways in a single class.

For example :

1:  class ParaConstructor  
2:  {  
3:       ParaConstructor()  
4:       {  
5:            System.out.println("Constructor with no parameters");  
6:       }  
7:       ParaConstructor(int first, int second)  
8:       {  
9:            System.out.println("Constructor with parameters");  
10:           System.out.println("The two integers entered are "+first+ " "+second);  
11:       }  
12:       public static void main(String[] args)  
13:       {  
14:            //Default constructor called  
15:            ParaConstructor obj1 = new ParaConstructor();  
16:            //Parameterized constructor called  
17:            ParaConstructor obj2 = new ParaConstructor(2, 5);  
18:       }  
19:  }  
Output :


Chaining of Constructors :

If any class inherits the properties of another class than that class can call the constructor of its parent class by the use of super keyword.

For example :

1:  class Parent  
2:  {  
3:       int length;  
4:       //a parameterized constructor  
5:       Parent(int len)  
6:       {  
7:            length = len;  
8:            System.out.println("The length in parent class is "+length);  
9:       }  
10:  }  
11:  class Child extends Parent  
12:  {  
13:       int breadth;  
14:       // a parameterized constructor of a child class  
15:       Child(int len, int br)  
16:       {  
17:            // call the parent class constructor  
18:            super(len);  
19:            breadth = br;  
20:       }  
21:       public static void main(String[] args)  
22:       {  
23:            Child obj = new Child(3, 6);  
24:       }  
25:  }  
Output :


Conclusion or all Properties of the Constructors :

1. Constructor is used for the automatic initialization of an object.

2. Every java program must contain a default constructor that will be without parameter and public type.

3. The name of the constructor is same as the name of the class.

4. A constructor has no return type and not even void because they implicitly return the class type itself.

5. A constructor can be private, public, default, protected.

6. A constructor never allowed static and final keyword with it.


2 comments :

  1. I found best article.thank you for sharing useful info.
    visit
    web programming tutorial
    welookups

    ReplyDelete
  2. I believe there are many more pleasurable opportunities ahead for individuals that looked at your site.

    python Training in Bangalore | python Training in Bangalore

    ReplyDelete