OOP 5. Constructor Overloading - TimeOfDay
- Introduce constructor overloading -- multiple constructors differing in number or types of parameters.
- Typically this is used to provide optional initialization parameters.
- Use of "this(...)" to call one constructor from another.
- The "this(...)" call must be first line a constructor.
- Using "this(...)" is much better than duplicating code.
Overloading constructors
It's common to overload constructors - define multiple constructors which differ in number and/or types of parameters. For example, exact hours are common, so an additional constructor could be defined which takes only the hour parameter. You can then set to minutes to a default value.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
// File : oop/timeofday/TimeOfDay1c.java // Purpose: A time-of-day class with constructor, which provides // a more convenient and conventional way to build objects. // The constructor is overloaded (more than one version) for // more convenience. // Author : Fred Swartz, 2005-05-04, Placed in public domain. public class TimeOfDay1c { public int hour; public int minute; //==================================================== constructor public TimeOfDay1c(int h, int m) { hour = h; minute = m; } //==================================================== constructor public TimeOfDay1c(int h) { hour = h; minute = 0; // Set minutes to 0. } } |
Calling one constructor from another using "this(...)"
Default parameters. It's very common for a constructor with fewer parameters to call a constructor with more parameters, supplying default values. For this usage, the constructors with fewer parameters will frequently consist of only the "this" call.
"this". Instead of calling the constructor with the class name, use
the keyword this
. The compiler matches "this" with
a constructor with the appropriate number and types of parameters, and
calls it.
Must be first. The "this" call must be the very first line of the constructor.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
// File : oop/timeofday/TimeOfDay1d.java
// Purpose: A time-of-day class with overloaded constructors.
// One constructor calls the other using "this".
// Author : Fred Swartz, 2007-02-27, Placed in public domain.
public class TimeOfDay1d {
public int hour;
public int minute;
//==================================================== constructor
public TimeOfDay1d(int h, int m) {
hour = h;
minute = m;
}
//==================================================== constructor
public TimeOfDay1d(int h) {
this(h, 0); // Call other constructor.
}
}
|
Hidden constructor call at the beginning of every constructor
The one essential way in which constructors differ from methods
is that the first statement of every constructor is either a call on the
constructor for the superclass (using super
) or a call to another
constructor in the same class (using this
).
Hidden call. You normally don't see the super call because the compiler automatically generates a call to the parameterless superclass constructor for you, but it's generated at the beginning of every constructor which doesn't explicitly call another constructor.
public TimeOfDay1d(int h, int m) { hour = h; minute = m; }
Is the same as.
public TimeOfDay1d(int h, int m) {
super(); // Call Object constructor.
hour = h;
minute = m;
}
"Copy" Constructors
Another reason to overload constructors is to provide a copy constructor,
which builds one object from the values of another similar object.
Anther way to do this is for a class to implement the clone()
method,
but we'll illustrate the copy constructor approach here.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
// File : oop/timeofday/TimeOfDay1d.java
// Purpose: A time-of-day class with overloaded constructors.
// Implements a copy constructor.
// Author : Fred Swartz, 2007-02-27, Placed in public domain.
public class TimeOfDay1e {
public int hour;
public int minute;
//==================================================== constructor
public TimeOfDay1e(int h, int m) {
hour = h;
minute = m;
}
//==================================================== constructor
public TimeOfDay1e(int h) {
this(h, 0); // Call other constructor.
}
//==================================================== constructor
public TimeOfDay1e(TimeOfDay1e other) {
this(other.hour, other.minute); // Call other constructor.
}
}
|
Questions
- What would happen if you called the following constructor?
TimeOfDay1c dentistAppointment = new TimeOfDay1c();