Search code examples
javagenericsabstract-data-type

Using Generics with Abstract Data Types in Java


For a CS class I need to solve an assigned problem using three data structures: Queue, PriorityQueue, and Stack. I wanted to write a single solution to the problem using an abstract data structure. I would implement the ADT with a wrapper class of each required data type. This is what I have so far:

An interface called Method:

public interface Method<E> {

    public abstract void add(E data);
    public abstract E remove();
    public abstract E peek();
    public abstract Iterator<E> Iterator();
}

And three wrapper classes that implement the interface. I called them QueueMethod, StackMethod, and PriorityQueueMethod. I'm having some trouble implementing the interface though. This is a start for the implementation that gives the error "Class is not abstract and does not override abstract method add(java.lang.Object)." As far as I can tell the signature of the two add methods are identical.

Here's the beginning QueueMethod wrapper class:

public class PriorityQueueMethod<T> implements Method {

    PriorityQueue<T> queue;

    public PriorityQueueMethod() {
        queue = new PriorityQueue<T>();
    }

    public void add(T data) {
        queue.offer(data);
    }
}

Solution

  • Add generic to the Method class you are implementing, like this:

    public class PriorityQueueMethod<T> implements Method<T>