Witscale Test Center

11.3 Method overloading and overriding > 11.3.3 Constructor overloading


11.3.3 Constructor overloading

Constructors can be overloaded just like the other methods of the class. The prominent reason of overloading a constructor is to pass different arguments that can be used while constructing an object. For example if we are constructing a car object, it is possible to create it using different arguments such as VIN (vehicle identification number) or car make and year. In that case, the Car class may have overloaded constructors such as:

 

public class Car {

   public Car(String vin) {}

   public Car(String make, int year) {}

}

 

All the rules that apply to overloaded methods are also applicable to constructors. You cannot have two constructors with same signature within a Java class.

 

Caution: Note that constructors do not have return type. Therefore, a class can have method, which takes same arguments as constructors with the same name. For example, following code is valid even if it appears otherwise.

 

 

public class Car {

   public Car(String vin) {}           // constructor

   public void Car(String vin) {}     // valid method

}

Invoking overloaded constructor with this


We saw earlier that the overloaded methods could call each other. Similarly, a constructor can call other overloaded constructors. However, the constructors are special methods; hence they cannot be called by their name. Instead, they are called with the help of this keyword. Figure 11.7 shows a Car class has three overloaded constructors.

 

Figure 11.7 Calling overloaded constructor from a constructor using the this

 

All three constructors take different combinations of arguments. Two of the constructors are calling the other one with this keyword. They need to make sure that appropriate arguments are passed while making the call.

Invoking superclass constructor with super


Constructors are not inherited like methods. However, you can call a constructor of the superclass of a class from the class’s constructor using super. Figure 11.8 shows the Car class and its superclass Automobile. The constructors that take different combinations of three arguments year, mileage and engineType are defined in the Automobile and Car class.

Figure 11.8 Calling the superclass constructor from a subclass constructor using super

 

The Car class constructor can call the Automobile constructor with the super keyword. It needs to make sure that appropriate arguments are passed while making the call.

The call to superclass constructor using super or a call to the overloaded constructor using this must always come as a first statement in the constructor body. If not the code fails to compile. In simple words, the constructor calls using this() or super() in the constructor body must appear as first statements.

 

 

Implicit call to the superclass constructor with super()

The explicit calling of constructors is simpler. However, the fact that makes constructors difficult to understand is the implicit calling. Since each Java class has some class as its superclass, when you construct an object, first the parental part of that object is created from the superclass. To do this Java compiler inserts a zero-argument call to the superclass constructor for you. In the following example, the compiler will put super() on the first line of Car constructor. Therefore when you create a Car object as new Car(1998); ,  the message “In Automobile constructor” is printed even if the Automobile constructor is not called explicitly.


                   

When the compiler puts an implicit call to the superclass constructor, the super class must have a zero-argument constructor. Otherwise, the compilation fails. Therefore, Automobile class must have a zero-argument constructor because Car constructor is going to call it implicitly. Now you may think the Car class compilation will fail if we remove the zero-argument constructor in Automobile. However, the twist here is that since “the compiler creates default constructor on the fly (see default constructor in chapter 3) whenever a Java class does not have any constructor”, the Automobile class without any constructor will actually have a zero-argument default constructor and hence the Car class will compile.

 


Compiler always puts the implicit call to zero-argument superclass constructor if the subclass constructor is not explicitly calling either its own constructor using this or the superclass constructor using super. Figure 11.9 illustrates the implicit calling of zero-argument constructor of a super class.

 

 

Figure 11.9 Implicit calling the zero-argument superclass constructor while creating a Car object as new Car(1998);

 

The Java compiler puts super() in Car constructor which calls the zero-argument constructor of Automobile. Since the Automobile constructor does not call any other constructor, the compiler will put implicit call super() in it as well. Automobile does not mention any superclass; therefore, its superclass is java.lang.Object. Therefore the implicit call super() in Automobile constructor will call the Object class’s zero-argument constructor.

When the compiler does not put the implicit call to the superclass constructor ?

Note that the compiler does not always put the super() in constructors of subclass. It makes exception in two cases. The implicit call super() is not created in subclass constructor if –

         The subclass constructor is explicitly calling the superclass’s constructor with super keyword.

         The subclass constructor is calling its other overloaded constructor with this keyword.

In simple words, the implicit call to super() is not created if the first line of constructor is call with super() or this(). Since the call to superclass constructor is mandatory, it puts it in a constructor that does not have super or this as its first statement. In the following code, if we constructor the Car object as new Car(1998), first the message “In Automobile Constructor” is printed followed by “In Car Constructor”.

 

 


Figure 11.10 Explicit and implicit calling of the superclass constructors while creating a Car object as new Car(1998);

 

Thus, the compiler tries its best to make sure that the superclass constructor is always invoked from the subclass constructor. If you are calling it explicitly, compiler does not do anything. However, if you are not, the compiler puts an implicit call super().

Things to remember while overloading constructors

Let us summarize the key points you need to remember about constructors.

 

         You can call overloaded constructors using the this keyword and superclass constructors using the super keyword.

         If you do not have any constructor in your Java class, the compiler creates a default constructor that takes no argument and has a call super() at its first line.

         Every constructor must have either a call to its its own overloaded constructors using this() or a call to its superclass constructor using super() at its very first line.

         If you (as a programmer) do not type-in the explicit constructor call, the compiler puts an implicit call super(). In that case, the superclass must have a zero-argument constructor (either typed-in by you or created by compiler) in it.