0

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?

3
  • When you write implements Stack you are in fact implementing Stack<Object>. Commented Oct 13, 2013 at 14:44
  • Be careful about reporting where errors are; it's usually best to just post the error message. The error is not in your interface, it's in your implementing class. Commented Oct 13, 2013 at 14:46
  • This is why you should always use the @Override annotation, your IDE would have inmidiatly informed you what the problem was Commented Oct 13, 2013 at 14:47

1 Answer 1

5

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>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.