Programming and Problem Solving with Java

(やまだぃちぅ) #1
11.5 T h e List Class Hierarchy and Abstract Classes | 549

ListWithSortis a List.SortedListis a List.ListWithSortis not a SortedList, and SortedListis
not a ListWithSort.
We could have organized the hierarchy using an abstract class. Recall from Chapter 7 that
an abstract class is a class that is headed by the wordabstractand leaves one or more meth-
ods incomplete. We cannot instantiate an abstract class. Rather, another class must extend
the abstract class and implement all of the abstract methods. In the preceding example, we
could have implemented the observers and iterator in the abstract class and left the imple-
mentation of the transformers to the derived class. Then the unsorted and sorted versions
of the list could both inherit from the abstract class.The documentation for the classes would
be as follows:


public abstract classList
{
publicList()
publicList(int maxItems)
public booleanisFull()
public booleanisEmpty()
public intlength()
public booleanisThere(String item)
public voidresetList()
publicString getNextItem()
public abstract voiddelete(String item)
public abstract voidinsert(String item)
}


public classUnsortedList extendsList
{
publicUnsortedList()
publicUnsortedList(int maxItems)
public voiddelete(String item)
public voidinsert(String item)
}


public classSortedList extendsList
{
publicSortedList()
publicSortedList(int maxItems)
public voiddelete(String item)
public voidinsert(String item)
}


public classListWithSort extendsUnsortedList
{
publicListWithSort()
publicListWithSort(int maxItems)
public voidselectSort()
}

Free download pdf