Overloading Constructors


Overloading Constructors

When an object is instantiated, the program might be able to supply multiple constructors based on the data for the object being created. For example, a payroll system might be able to create an Employee object when it knows all of the basic data about the person: name, starting salary, and date of birth. Sometimes the system might not know the starting salary or the date of birth.

Code 1 shows four overloaded constructors for the Employee class. The first constructor (Lines 7–11) initializes all instance variables. In the second one (Lines 12–14), the date of birth is not provided. The this reference is being used as a forwarding call to another constructor (always within the same class); in this case, the first constructor. Likewise, the third constructor (Lines 15–17) calls the first constructor passing in the class constant BASE_SALARY. The fourth constructor (Lines 18–20) calls the second constructor passing the BASE_SALARY, which, in turn, calls the first constructor passing null for the date of birth.

Code 1 Example Overloading Constructors

1 public class Employee {

2 private static final double BASE_SALARY = 15000.00;

3 private String name;

4 private double salary;

5 private Date birthDate;


7 public Employee(String name, double salary, Date DoB) {

8 this.name = name;

9 this.salary = salary;

10 this.birthDate = DoB;

11 }

12 public Employee(String name, double salary) {

13 this(name, salary, null);

14 }

15 public Employee(String name, Date DoB) {

16 this(name, BASE_SALARY, DoB);

17 }

18 public Employee(String name) {

19 this(name, BASE_SALARY);

20 }

21 // more Employee code…

22 }

The this keyword in a constructor must be the first line of code in the constructor. There can be more initialization code after the this call, but not before.

Constructors Are Not Inherited

Although a subclass inherits all of the methods and variables from a parent class, it does not inherit constructors. There are only two ways in which a class can gain a constructor; either you write the constructor, or, because you have not written any constructors, the class has a single default constructor. A parent constructor is always called in addition to a child constructor. This is described in detail later in this module.

Invoking Parent Class Constructors

Like methods, constructors can call the non-private constructors of its immediate superclass.

Often you define a constructor that takes arguments and you want to use those arguments to control the construction of the parent part of an object. You can invoke a particular parent class constructor as part of a child class initialization by using the keyword super from the child constructor’s first line. To control the invocation of the specific constructor, you must provide the appropriate arguments to super(). When there is no call to super with arguments, the parent constructor with zero arguments is called implicitly. In this case, if there is no parent constructor with zero arguments, a compiler error results.

The call to super() can take any number of arguments appropriate to the various constructors available in the parent class, but it must be the first statement in the constructor.

Assuming that the Employee class has the set of constructors that were defined in the Code 1, then the following constructors in Manager might be defined. The constructor on Line 12 is illegal because the compiler inserts an implicit call to super(), and the Employee class has not provided a constructor without arguments.

1 public class Manager extends Employee {

2 private String department;


4 public Manager(String name, double salary, String dept) {

5 super(name, salary);

6 department = dept;

7 }

8 public Manager(String name, String dept) {

9 super(name);

10 department = dept;

11 }

12 public Manager(String dept) { // This code fails: no super()

13 department = dept;
14 }

15 }

When used, you must place super or this in the first line of the constructor. If you write a constructor that has neither a call to super(…) nor this(…), the compiler inserts a call to the parent class constructor automatically, with no arguments. Other constructors can also call super(…) or this(…), invoking a chain of constructors. What happens ultimately is that the parent class constructor (or possibly several) executes before any child class constructor in the chain. Anil Kumar (tcฺanil@yahooฺin) has a non-transferable license


Comments are closed.