Implementation Interface Impact Name

I spent several hours reading the Java / Stackoverflow documentation on generics and type wiping, but I still don't fully understand this topic. The problem is the implementation of the following simple interface:

public interface Stack {
void push(Object element); }

I implemented this in a simple general class:

public class NodeStack<T> implements Stack {
private Node<T> top;
private int size;

public void push(T value) {
    if(this.size == 0) {
        this.top = new Node<>(value, null);
    } else {
        this.top = new Node<>(value, this.top);
    }
    this.size++;}
}

When I try to compile this, I get compiler error messages:

NodeStack.java:10: error: NodeStack is not abstract and does not override abstract method push(Object) in Stack
public class NodeStack<T> implements Stack {
   ^
NodeStack.java:63: error: name clash: push(T) in NodeStack and push(Object) in Stack have the same erasure, yet neither overrides the other

    public void push(T value) {
                ^
where T is a type-variable:
    T extends Object declared in class NodeStack
2 errors

I think this problem occurs due to type erasure - the push method in the class and the push method in the interface have the same signature after the type is erased. I do not understand why this will be a problem. An implementation of the push method must have the same signature as the interface method, right? What am I missing here? Any help is appreciated.

+4
3

, T, .

public interface Stack<T> 
{ 
    void push(T element); 
}

NodeStack

public class NodeStack<T> implements Stack<T> {
+6

push push .

, , .

, /, . , , - , .

, , T, , , , .

+3

: void push(Object element);, Object, . Object. - , NodeStack, :

NodeStack is not abstract and does not override abstract method push(Object) in Stack

:

, .

Object T. .

+1

Source: https://habr.com/ru/post/1680763/


All Articles