Java Reference
In-Depth Information
Like most classes, LList has data fields that are private. The client cannot access these fields
directly by name. The class designer must decide whether to provide public methods that give the
client indirect access to the data fields. In the case of LList , the public method getLength enables
the client to get the length of the list. The client, however, cannot directly change the list's length.
Only other member methods, such as add and remove , can alter the length. In addition, LList
denies the client access to the field firstNode by not providing public accessor or mutator methods
for this field. This design is appropriate, as firstNode is an implementation detail that should be
hidden from the client.
17.6
The excerpt of the class LList given in Listing 17-1 shows aspects of the class that are relevant to
this discussion. Each node is represented by the private class Node , which is defined within LList
and hidden from the client. The method getNodeAt facilitates the implementation of other mem-
ber methods by returning a reference to the node at a given position. We do not want the client to
have access to this node, since it is part of the underlying representation of the list, so we make
the method private.
LISTING 17-1 Relevant aspects of the class LList
public class LList<T> implements ListInterface<T>
{
private Node firstNode; // reference to first node
private int numberOfEntries;
public LList()
{
clear();
} // end default constructor
public final void clear()
{
firstNode = null ;
numberOfEntries = 0;
} // end clear
public int getLength()
{
return numberOfEntries;
} // end getLength
< Implementations of the public methods add , remove , replace , getEntry , contains ,
isEmpty , and toArray go here. >
. . .
// Returns a reference to the node at a given position.
private Node getNodeAt( int givenPosition)
{
. . .
} // end getNodeAt
Search WWH ::




Custom Search