Stack Class That Inherits from List<T>

Figures 21.10 and 21.11 create and manipulate a stack class that extends the List<T> class of Fig. 21.3. We want the stack to have methods push, pop, isEmpty and print. Essentially, these are the List<T> methods insertAtFront, removeFromFront, isEmpty and print. Of course, class List<T> contains other methods (such as insertAtBack and removeFromBack) that we would rather not make accessible through the public interface to the stack class. It’s important to remember that all methods in List<T>’s public interface class also are public methods of the subclass StackInheritance<T> (Fig. 21.10). Each method of StackInheritance<T> calls the appropriate List<T> method—for example, method push calls insertAtFront ...

Get Java™ How To Program (Early Objects), Tenth Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.