Here is the LinkedList class from the Chapter 2 exercises as an
interface:


interface LinkedList {

    /** Return the object at this node in the list */
    Object getObject();

    /** Return the next node in the list, or null if this is the last */
    LinkedList getNextNode();

    /** Set the next node in the list to the given node */
    void setNextNode(LinkedList newNext);

    /** Count the number of nodes in the list starting from this node */
    int numNodes();
}


And here an implementation of that interface:


class LinkedListImpl implements LinkedList {

    private Object obj;
    private LinkedList next;

    public LinkedListImpl(Object what) {
	obj = what;
    }

    public LinkedListImpl(Object what, LinkedList list) {
	obj = what;
	next = list;
    }

    public Object getObject() {
	return obj;
    }

    public LinkedList getNextNode() {
	return next;
    }

    public void setNextNode(LinkedList newNext) {
	next = newNext;
    }

    public int numNodes() {
	int count = 1;
	for (LinkedList node = next; node != null; node = node.getNextNode())
	    count++;
	return count;
    }

    public String toString() {
	String desc = "(";
	for (LinkedList node = this; node != null; node = node.getNextNode()) {
	    desc += node.getObject();
	    if (node.getNextNode() != null)
		desc += ", ";
	}
	desc += ")";
	return desc;
    }
}


Separating the LinkedList class into an interface and an
implementation like above does not, however, seem to produce a clear
design benefit.  The original LinkedList class implemented a simple,
specific contract, and it is not expected that other classes will want
to implement LinkedList's interface differently, to provide a linked
list with a different behavior.
