javastacksupertype

How to fix the error about supertype when implementing a method with parameter E e?


I need to create an array-based stack that gets the method from an interface. There I want to implement the push(E e) method from the interface, but I am getting following error:

The method push(E) of type ArrayStack<E> must override or implement a supertype method
public interface Stack<E> extends BasicCollection {

   public E top() throws EmptyStackException;

   public void push(E e);

   public E pop() throws EmptyStackException;

}
@Override
   public void push(E e) {
       if(size == arrayCapacity) {
           array = Arrays.copyOf(array, array.length * 2);
       }
       array[size] = e;
       size += 1;
   }

How can I solve this? It has the same parameter as in the interface. What is wrong?


Solution

  • When you write implements Stack you are using a raw type.

    See What is a raw type and why shouldn't we use it?

    Because you haven't specified the generic type E in Stack<E>, the base type Object is used, as if you wrote implements Stack<Object>. That means that the class is expected to implement the method push(Object) instead of push(E).

    Presumably you meant

    class ArrayStack<E> implements Stack<E>