Java Reference
In-Depth Information
The line with
this
is an invocation of the constructor with the following heading:
public
HourlyEmployee(String theName, Date theDate,
double
theWageRate,
double
theHours)
The restrictions on how you can use the base class constructor call
super
also apply
to the
this
constructor. You cannot use an instance variable as an argument to
this
.
Also, any call to the constructor
this
must always be the first action taken in a con-
structor definition. Thus, a constructor definition cannot contain both an invocation
of
super
and an invocation of
this
. If you want to include both a call to
super
and a
call to
this
, use a call with
this
, and have the constructor that is called with
this
have
super
as its first action.
Call to Another Constructor in the Same Class
Within the definition of a constructor for a class, you can use
this
as a name for another
constructor in the same class. Any invocation of
this
must be the first action taken by the
constructor.
EXAMPLE
public
HourlyEmployee()
{
this
("No name",
new
Date("January", 1, 1000), 0, 0);
}
TIP: An Object of a Derived Class Has More than One Type
An object of a derived class has the type of the derived class, and it also has the type
of the base class, and more generally, has the type of every one of its ancestor classes.
For example, consider the following copy constructor definition from the class
HourlyEmployee
(Display 7.3):
public
HourlyEmployee(HourlyEmployee originalObject)
{
super
(originalObject);
wageRate = originalObject.wageRate;
hours = originalObject.hours;
}
(continued)