Java Reference
In-Depth Information
We now use an inheritance hierarchy containing types of
employees
in a company's payroll
application to discuss the relationship between a superclass and its subclass. In this com-
pany,
commission employees
(who will be represented as objects of a superclass) are paid a
percentage of their sales, while
base-salaried commission employees
(who will be represented
as objects of a subclass) receive a base salary
plus
a percentage of their sales.
We divide our discussion of the relationship between these classes into five examples.
The first declares class
CommissionEmployee
, which directly inherits from class
Object
and declares as
private
instance variables a first name, last name, social security number,
commission rate and gross (i.e., total) sales amount.
The second example declares class
BasePlusCommissionEmployee
, which also directly
inherits from class
Object
and declares as
private
instance variables a first name, last
name, social security number, commission rate, gross sales amount
and
base salary. We
create this class by
writing every line of code
the class requires—we'll soon see that it's much
more efficient to create it by inheriting from class
CommissionEmployee
.
The third example declares a new
BasePlusCommissionEmployee
class that
extends
class
CommissionEmployee
(i.e., a
BasePlusCommissionEmployee
is a
CommissionEm-
ployee
who also has a base salary). This
software reuse lets us write much less code
when
developing the new subclass. In this example, class
BasePlusCommissionEmployee
attempts to access class
CommissionEmployee
's
private
members—this results in compi-
lation errors, because the subclass
cannot
access the superclass's
private
instance variables.
The fourth example shows that if
CommissionEmployee
's instance variables are
declared as
protected
, the
BasePlusCommissionEmployee
subclass
can
access that data
directly. Both
BasePlusCommissionEmployee
classes contain identical functionality, but
we show how the inherited version is easier to create and manage.
After we discuss the convenience of using
protected
instance variables, we create the
fifth example, which sets the
CommissionEmployee
instance variables back to
private
to
enforce good software engineering. Then we show how the
BasePlusCommissionEm-
ployee
subclass can use
CommissionEmployee
's
public
methods to manipulate (in a con-
trolled manner) the
private
instance variables inherited from
CommissionEmployee
.
We begin by declaring class
CommissionEmployee
(Fig. 9.4). Line 4 begins the class dec-
laration and indicates that class
CommissionEmployee
extends
(i.e.,
inherits from
) class
Ob-
ject
(from package
java.lang
). This causes class
CommissionEmployee
to inherit the class
Object
's methods—class
Object
does not have any fields. If you don't explicitly specify
which class a new class extends, the class extends
Object
implicitly. For this reason, you
typically will not include “
extends Object
” in your code—we do so in this one example
only for demonstration purposes.
Overview of Class
CommissionEmployee
's Methods and Instance Variables
Class
CommissionEmployee
's
public
services include a constructor (lines 13-34) and
methods
earnings
(lines 87-90) and
toString
(lines 93-101). Lines 37-52 declare
pub-
lic
get
methods for the class's
final
instance variables (declared in lines 6-8)
firstName
,
lastName
and
socialSecurityNumber
. These three instance variables are declared
final
because they do not need to be modified after they're initialized—this is also why we do