UML parent classes

Our recitation uses an IQueue interface that defines a set of common methods between the classes we will be implementing. Below that we have an AQueue abstract class. This recitation approaches a particular use case for abstract classes.

If you examine the method descriptions in the interface, you’ll notice that there are several pairs of methods that do the same thing.

Method One

Method Two

Action

add(E item)

offer(E item)

places items into the queue

remove()

poll()

removes items from the queue

element()

peek()

looks at the item that is currently at the start of the queue

The only difference is that the methods in the second column either return false or null (they fail silently) when they are unable to perform the actions that are described, while the methods in the first column throw exceptions. The only other method in the AQueue class is isEmpty() and this method can also depend on method in the interface.

By implementing one of these methods in terms of the other, we can avoid code duplication. We can take this a step farther by implementing these methods in a common abstract base class instead of the concrete child classes.

We’ve implemented the first method to help get you started:

Method in AQueue