0

I have the following code:

public interface StackInterface<T> {
   public T pop();
   public void push(T n);
}


public class myStack<T> implements StackInterface<Node<T>> {
  Node<T> head;
  Node<T> next;
  Node<T> tail;
  public myStack(T t) {
        head = new Node<T>(t);
        head.next = null;
        tail=head;
  }

public myStack() {
    head = null;
    tail=head;
}

public Node<T> pop() {
    if(head==null) {
        return null;
    }
    Node<T> t= head;
    head=head.next;
    return t;
}

public void push(T n) {
    Node<T> t = head;
    head = new Node<T>(n);
    head.next = t;
}

}

This code shows the following errors:

on the class declaration line; it says that it does not implement the method public void push(T n); and on public void push(T n) line it says:

The method push of myStack has the same erasure as push of StackInterface but does not override it.

The method prototypes are identical; adding @Override does nothing. Why is this happening?

Dax Duisado
  • 197
  • 2
  • 11
  • Looks like push signature needs to be `public void push(Node n)`? – Radiodef Oct 28 '13 at 03:25
  • Thanks for the reply; that got rid of the error...but still am not at my intended functionality. Should I add another generic, perhaps U to the interface, myStack and Node to get it to work? – Dax Duisado Oct 28 '13 at 03:29
  • Well it seems like you are trying to make a LinkedList analog with the pushing/popping but I don't see a list unless I'm missing something. Maybe you should edit the question so it reflects the new problem since I'm not totally sure what you're wanting to do. Unless Node is your own class and is some kind of list itself? – Radiodef Oct 28 '13 at 03:41

2 Answers2

1

You need to implements this way, then your templates matches.

public class myStack<T> implements StackInterface<T>
Chowdappa
  • 1,580
  • 1
  • 15
  • 31
  • +1 the use of Node is an implementation choice, and should not be visible through the API. You have a stack of T, not of Node – Bohemian Oct 28 '13 at 04:06
0

Because you are implementing StackInterface<Node<T>>, the push method needs to be

public void push(Node<T> n) {
}
Ean V
  • 5,091
  • 5
  • 31
  • 39