Java Reference
In-Depth Information
BasePlusCommissionEmployee.java:56: error: lastName has private access in
CommissionEmployee
"base-salaried commission employee", firstName, lastName,
^
BasePlusCommissionEmployee.java:57: error: socialSecurityNumber has private
access in CommissionEmployee
"social security number", socialSecurityNumber,
^
BasePlusCommissionEmployee.java:58: error: grossSales has private access in
CommissionEmployee
"gross sales", grossSales, "commission rate", commissionRate,
^
BasePlusCommissionEmployee.java:58: error: commissionRate has private access
inCommissionEmployee
"gross sales", grossSales, "commission rate", commissionRate,
^
Fig. 9.8
| private
superclass members cannot be accessed in a subclass. (Part 3 of 3.)
A Subclass's Constructor Must Call Its Superclass's Constructor
Each subclass constructor must implicitly or explicitly call one of its superclass's construc-
tors to initialize the instance variables inherited from the superclass. Lines 14-15 in
Base-
PlusCommissionEmployee
's six-argument constructor (lines 9-23) explicitly call class
CommissionEmployee
's five-argument constructor (declared at lines 13-34 of Fig. 9.4) to
initialize the superclass portion of a
BasePlusCommissionEmployee
object (i.e., variables
firstName
,
lastName
,
socialSecurityNumber
,
grossSales
and
commissionRate
). We
do this by using the
superclass constructor call syntax
—keyword
super
, followed by a set
of parentheses containing the superclass constructor arguments, which are used to initial-
ize the superclass instance variables
firstName
,
lastName
,
socialSecurityNumber
,
grossSales
and
commissionRate
, respectively. If
BasePlusCommissionEmployee
's con-
structor did not invoke the superclass's constructor explicitly, the compiler would attempt
to insert a call to the superclass's default or no-argument constructor. Class
Commission-
Employee
does not have such a constructor, so the compiler would issue an error. The ex-
plicit superclass constructor call in lines 14-15 of Fig. 9.8 must be the
first
statement in
the constructor's body. When a superclass contains a no-argument constructor, you can
use
super()
to call that constructor explicitly, but this is rarely done.
Software Engineering Observation 9.6
You learned previously that you should not call a class's instance methods from its
constructors and that we'll say why in Chapter 10. Calling a superclass constructor from
a subclass constructor does not contradict this advice.
BasePlusCommissionEmployee
Methods
Earnings
and
toString
The compiler generates errors for line 46 (Fig. 9.8) because
CommissionEmployee
's in-
stance variables
commissionRate
and
grossSales
are
private
—subclass
BasePlusCom-
missionEmployee
's methods are
not
allowed to access superclass
CommissionEmployee
's
private
instance variables. We used
red text
in Fig. 9.8 to indicate erroneous code. The
compiler issues additional errors at lines 56-58 of
BasePlusCommissionEmployee
's
to-
String
method for the same reason. The errors in
BasePlusCommissionEmployee
could
have been prevented by using the
get
methods inherited from class
CommissionEmployee
.