next up previous index
Next: Implementation Up: Stacks Previous: Simple Implementation   Index


Interfaces

Notice that clients of the SimpleStack class only have access to the stack through the public methods isEmpty, isFull, push and pop. The actual stack array and the pointer top are private and cannot be directly manipulated by the client. In fact it is not necessary to implement a stack using an array. We shall see how it can also be implemented using a linked structure.

From the client's point of view, details of the implementation are secondary. The primary consideration is that it provides the necessary functionality, so that it interfaces correctly with the client's program. We can express these ideas by using the Java construct of an interface. A simple interface for a Stack of objects is shown in Figure 2.2.

Figure 2.2: An interface for the Stack abstract data type.
public interface Stack {
    public boolean isEmpty();
    public void push(Object item);
    public Object pop();
}

In the specification of the interface, only the methods, their parameters and return types are shown. You will see that the isFull method has now disappeared. This is because we want the stack to have a adjustable size so that, subject to general memory limitations, it is never full. The isEmpty method remains. We can never pop an item off an empty stack. The interface requires that pop throws a NoSuchElementException if an attempt is made to do so.


next up previous index
Next: Implementation Up: Stacks Previous: Simple Implementation   Index
Peter Williams 2005-06-07