Java Reference
In-Depth Information
to
entryNode.setData(firstNode.getData());
Project 2 at the end of this chapter asks you to complete these revisions to LinkedBag .
3.27
As a class within a package. After we modify Node and LinkedBag as just described, Node could
remain as a private inner class. Since Node is an implementation detail that we want to hide, making
it an inner class is appropriate. But if we ever changed our minds and wanted to define Node outside
of LinkedBag , we could do so while retaining the modifications to LinkedBag made in the previous
segment. We could—with a few changes—make Node accessible only within a package, or we
could even make it a public class.
To transform Node , as given in Listing 3-4, into a class accessible only by other classes in its
package, you first omit all the access modifiers except the ones for the data fields. You then add <T>
after each occurrence of Node within the class definition, except when it is used as a constructor
name. The revised class appears in Listing 3-5.
LISTING 3-5
The class Node with package access
package BagPackage;
class Node<T>
{
private T data;
private Node<T> next;
Node(T dataPortion) // the constructor's name is Node, not Node<T>
{
this (dataPortion, null );
} // end constructor
Node(T dataPortion, Node<T> nextNode)
{
data = dataPortion;
next = nextNode;
} // end constructor
T getData()
{
return data;
} // end getData
void setData(T newData)
{
data = newData;
} // end setData
Node<T> getNextNode()
{
return next;
} // end getNextNode
void setNextNode(Node<T> nextNode)
Search WWH ::




Custom Search