Monday, December 31, 2012

How Constructor Chaining works in Java - Example

How to call one constructor from other constructor in Java or What is Constructor Chaining in Java is one of the tricky questions in Java interviews. Well you can use this keyword to call one constructor from other constructor of same class, if you want to call constructor from based class or super class than you can use super keyword. Calling one constructor from other is called Constructor chaining in Java. Constructors can call each other automatically or explicitly using this() and super() keywords. this() denotes a no argument constructor of same class and super() denotes a no argument or default constructor of parent class. Also having multiple constructor in same class is known as constructor overloading in Java.

How Constructor chaining works in Java

How Constructor Chaining works in JavaAs I said Constructor can be call other constructor automatically or explicitly. If you do not call any constructor either from same class or super class than Java compiler will call super() i.e. no argument constructor of Parent class. Also call to another constructor must be first line in Constructor. Suppose you want to call another constructor of same class which accept an int argument, you can use this(1) to call that constructor. Similarly if that constructor is in super class, you can use super(1) to call that constructor. You can not call more than one constructor from a constructor in Java because every constructor call must be first statement. Also if you don't declare any constructor in your class, Java will add default or no argument constructor in your class. In next section we will see an example of how to call one constructor from other in Java.

Constructor Chaining Example in Java
Here is a code example of constructor chaining in Java. In this Java program we show how to call constructor using both this and super keyword. this() calls constructor from same class and super() calls constructor from parent class in Java.

/**
 *
 * Java program to demonstrate How constructor chaining in Java.
 * When one constructor calls other in Java then it referred as Constructor chaining.
 * this and super keywords are used to call constructor from another constructor.
 *
 * @author http://java67.blogspot.com
 */

public class ConstructorChaining{

    public static void main(String args[]) {
           // testing constructor chaining in Java
           Derived sub = new Derived("Test");
    }
}

class Base{
    protected String name;
 
    public Base(){
        this(""); //calling one arg constructor of same class
        System.out.println("Inside no argument constructor of Base class");
    }
 
    public Base(String name){
        this.name = name;
        System.out.println("One arg constructor of Base class");
    }
}

class Derived extends Base{
 
   public Derived(){
       System.err.println("Inside no argument constructor of Derived class");
   }
 
   public Derived(String name){
       super(name); //calling one argument constructor of super class
       System.out.println("Inside one arg constructor from Derived class");
   }
}

Output
One arg constructor of Base class
Inside one arg constructor from Derived class


In this example we create an instance of Derived class by calling constructor of Derived class which accept a String argument, that constructor than class to one argument constructor of super class. This is an example of explicitly calling one constructor from other in Java. To demonstrate automatic constructor chaining let's see another example

Derived sub = new Derived();

Output
One arg constructor of Base class
Inside no argument constructor of Base class
Inside no argument constructor of Derived class

In this example we are calling no argument constructor of Derived class which automatically calls no argument constructor of Super class, which is Base in this case, which in turn call String argument constructor of Base class. Since there is no explicit constructor call on Derived class no argument constructor it by default calls to super().

Important points about Constructor chaining in Java
Here are few worth noting points about Constructor chaining in Java, which is core of calling one constructor from other in Java program :

1) Call to another constructor of same class or parent class must be first statement in constructor.

2) If you do not call another constructor either from parent class or same class than Java calls default or no argument constructor of super class.

3) If you do not declare any constructor in Java then Java compiler adds a default or no argument constructor in your class, which by default calls no argument constructor of super class. If you don't have a no argument constructor in super class than this would result in compile time error. That's why we should always have a no argument constructor in our class.

That's all on What is Constructor Chaining in Java and How constructor chaining works in Java. Just remember that this() and super() can be used to explicitly call constructor of same class and constructor of parent class in Java. It's also worth remembering points mentioned about special behaviors of Constructor in Java.

Happy New Year to All of You !!!

Other Java programming tutorials for programmers

No comments:

Post a Comment

Java67 Headline Animator