Java Reference
In-Depth Information
Supposeyouwanttologeachappointmentinafile.Becausealoggingcapabilityis
endar
class, which adds logging behavior in overriding
addAppt()
and
ad-
dAppts()
methods.
Listing 2-30.
Extending the appointment calendar class
public class LoggingApptCalendar extends ApptCalendar
{
// A constructor is not necessary because the Java com-
piler will add a
// noargument constructor that calls the superclass's
noargument
// constructor by default.
@Override
public void addAppt(Appt appt)
{
Logger.log(appt.toString());
super.addAppt(appt);
}
@Override
public void addAppts(Appt[] appts)
{
for (int i = 0; i < appts.length; i++)
Logger.log(appts[i].toString());
super.addAppts(appts);
}
}
void log(String msg)
class method logs a string to a file (the details are un-
important). Notice the use of
toString()
to convert an
Appt
object to a
String
object, which is then passed to
log()
.
Although this class looks okay, it does not work as you might expect. Suppose you
instantiatethisclassandaddafew
Appt
instancestothisinstancevia
addAppts()
,
in the following manner: