Java Reference
In-Depth Information
Supposeyouwanttologeachappointmentinafile.Becausealoggingcapabilityis
not provided, you extend ApptCalendar with Listing 2-30 ' s LoggingApptCal-
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);
}
}
Listing 2-30 ' s LoggingApptCalendar class relies on a Logger class whose
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:
 
Search WWH ::




Custom Search