Java Reference
In-Depth Information
public class
Employee
implements
Cloneable
{
private
String name;
private
Date hireDate;
...
public
Object clone()
{
try
{
Employee copy =
(Employee)
super
.clone();
copy.hireDate =
(Date)hireDate.clone();
return
copy;
}
catch
(CloneNotSupportedException e)
{
//This should not happen.
return null
;
//To keep compiler happy.
}
}
}
18. The heading of the class definition changes to what is shown in the following
and the method
clone
shown there is added. Note that you do not catch a
CloneNotSupportedException
because any such thrown exception in
super.
clone
is caught inside the base class method
super.clone
. The version of
HourlyEmployee
for this chapter on the accompanying website includes this
definition of
clone
.
public class
HourlyEmployee
extends
Employee
implements
Cloneable
extra code
on website
{
private double
wageRate;
private double
hours;
...
public
Object clone()
{
HourlyEmployee copy =
(HourlyEmployee)
super
.clone();
return
copy;
}
}
19. It would still be legal. An outer class has access to all the private members of an
inner class.
20. Yes, they can be omitted, but the reason is that it indicates package access, and in
a private inner class, all privacy modifiers, including package access, are equivalent
to public. (Note that the situation for public inner classes will be different.)