Little
Little

Reputation: 3477

issue with java generics in an interface

I am implementing an interface that can manage a stack, I have the following code:

public interface Stack <E>{
    public int size();
    public boolean isEmpty();
    public E top();
    public void push(E item);    //error here

and the class that implements this interface has:

public class StackArray<E> implements Stack{
    private E array[];
    private int top=-1;
    public StackArray(int n){
        array=(E[]) Array.newInstance(null, n); ////not quite sure about this
    }
    //more code
    public void push(E item){
        if (top==array.length-1) System.out.println("stack is full");
        else{
            top++;
            array[top]=item;
        }
    }

the error that I got is the I am not overriding the push method, I see that both have the same signature, but I am not quite sure.

how can I fix that?

Upvotes: 0

Views: 53

Answers (1)

You're not binding the E parameter in StackArray to the E parameter in Stack. Use this declaration:

public class StackArray<E> implements Stack<E>

Upvotes: 5

Related Questions