Java Reference
In-Depth Information
public void
setTitle(String theTitle)
{
title = theTitle;
}
public
String getName()
{
return
(title +
super
.
getName());
}
}
7. It would be legal to add
crazyMethod
to the class
Employee
. It would not be legal
to add
crazyMethod
to the class
HourlyEmployee
because, although the class
HourlyEmployee
has an instance variable
name
,
name
is private in the base class
Employee
and so cannot be accessed by name in
HourlyEmployee
.
8. Yes, it would be legal as long as
name
is marked
public
in the base class
Employee
.
9. Yes, it would be legal as long as
name
is marked
protected
in the base class
Employee
.
10. Package access is more restricted. Anything allowed by package access is also
allowed by protected access, but protected access allows even more.
11. Yes, it is legitimate.
12. Yes, it is legitimate.
13. No, it is not legitimate. The compiler will give an error message saying
doStuff()
is protected in
B
.
14.
public
String toString()
{
return
(
super
.toString()
+ "\n$" + salary + " per year");
}
15.
public boolean
equals(HourlyEmployee other)
{
return
(
super
.equals(other)
&& wageRate == other.wageRate
&& hours == other.hours);
}
A better definition of
equals
for the class
HourlyEmployee
is given in Display 7.10 .
16. It is not legal. You cannot use
super
in this way.
super.toString()
as used here
refers to
toString()
in the class
Employee
and can only be used in definitions of
classes derived from
Employee
. Moreover, you cannot have a calling object, such
as
joe
, before
super
, so this is even illegal if you add
extends
Employee
to the
first line of the class definition.