Program 21.1: A String stack

Stacks in Java are a subclass of java.util.Vector, and they share with Vector the weakness of requiring the objects stored in the stack to be objects. If your application only needs the stack to hold one kind of data, you may wish to subclass stack. For example program 21.1 is a subclass of Stack that holds only Strings.

public class StringStack extends java.util.Stack {

  public String peek() {
    return (String) super.peek();
  }

  public String pop() {
    return (String) super.pop();
  }

}

Copyright 1996 Elliotte Rusty Harold
elharo@sunsite.unc.edu
This Chapter
Examples
Home