Java Reference
In-Depth Information
The problem is that implementation inheritance breaks encapsulation. The subclass
reliesonimplementationdetailsinthesuperclass.Ifthesedetailschangeinanewver-
sion of the superclass, the subclass might break, even if the subclass is not touched.
Forexample,supposeyouhavepurchasedalibraryofJavaclasses,andoneofthese
classes describes an appointment calendar. Although you do not have access to this
class's source code, assume that
Listing 2-29
describes part of its code.
Listing 2-29.
An appointment calendar class
public class ApptCalendar
{
private final static int MAX_APPT = 1000;
private Appt[] appts;
private int size;
public ApptCalendar()
{
appts = new Appt[MAX_APPT];
size = 0; // redundant because field automatically
initialized to 0
// adds clarity, however
}
public void addAppt(Appt appt)
{
if (size == appts.length)
return; // array is full
appts[size++] = appt;
}
public void addAppts(Appt[] appts)
{
for (int i = 0; i < appts.length; i++)
addAppt(appts[i]);
}
}
pointmentdescribedbyan
Appt
instance.Forthisdiscussion,
Appt
'sdetailsareirrel-
evant—it could be as trivial as
class Appt {}
.