Java Reference
In-Depth Information
99
100
101
102
}
// end class CommissionEmployee
"gross sales"
, grossSales,
"commission rate"
, commissionRate);
}
Fig. 9.4
| CommissionEmployee
class represents an employee paid a percentage of gross sales.
(Part 3 of 3.)
Class
CommissionEmployee
's Constructor
Constructors are
not
inherited, so class
CommissionEmployee
does not inherit class
Object
's constructor. However, a superclass's constructors are still available to be called by
subclasses. In fact, Java requires that
the first task of any subclass constructor is to call its direct
superclass's constructor
, either explicitly or implicitly (if no constructor call is specified), to
ensure that the instance variables inherited from the superclass are initialized properly. The
syntax for calling a superclass constructor explicitly is discussed in Section 9.4.3. In this
example, class
CommissionEmployee
's constructor calls class
Object
's constructor implic-
itly. If the code does not include an explicit call to the superclass constructor, Java
implic-
itly
calls the superclass's default or
no-argument
constructor. The comment in line 17 of
Fig. 9.4 indicates where the implicit call to the superclass
Object
's default constructor is
made (you do
not
write the code for this call).
Object
's default constructor does nothing.
Even if a class does not have constructors, the default constructor that the compiler im-
plicitly declares for the class will call the superclass's default or no-argument constructor.
After the implicit call to
Object
's constructor, lines 20-22 and 25-27 validate the
grossSales
and
commissionRate
arguments. If these are valid (that is, the constructor
does not throw an
IllegalArgumentException
), lines 29-33 assign the constructor's
arguments to the class's instance variables.
We did not validate the values of arguments
firstName
,
lastName
and
socialSecu-
rityNumber
before assigning them to the corresponding instance variables. We could val-
idate the first and last names—perhaps to ensure that they're of a reasonable length.
Similarly, a social security number could be validated using regular expressions
(Section 14.7) to ensure that it contains nine digits, with or without dashes (e.g.,
123-45-
6789
or
123456789
).
Class
CommissionEmployee
's
earnings
Method
Method
earnings
(lines 87-90) calculates a
CommissionEmployee
's earnings. Line 89
multiplies the
commissionRate
by the
grossSales
and returns the result.
Class
CommissionEmployee
's
toString
Method and the
@Override
Annotation
Method
toString
(lines 93-101) is special—it's one of the methods that
every
class inher-
its directly or indirectly from class
Object
(summarized in Section 9.6). Method
toString
returns a
String
representing an object. It's called implicitly whenever an object must be
converted to a
String
representation, such as when an object is output by
printf
or out-
put by
String
method
format
via the
%s
format specifier. Class
Object
's
toString
meth-
od returns a
String
that includes the name of the object's class. It's primarily a placeholder
that can be
overridden
by a subclass to specify an appropriate
String
representation of the
data in a subclass object. Method
toString
of class
CommissionEmployee
overrides (rede-
fines) class
Object
's
toString
method. When invoked,
CommissionEmployee
's
toString