Home > Blockchain >  Having to use "Object" instead of "T" when inheriting from a class [duplicate]
Having to use "Object" instead of "T" when inheriting from a class [duplicate]

Time:10-02

In trying to make a LinkedList collection for school, however my teacher and I can't figure out why I can't use "T" when declaring a target; the compiler says I need to use "Object."

public class LinkedListCollection<T> implements CollectionInterface {
    int size = 0;
    Node<T> head;
    /**
     * InnerLinkedListCollection
     */
    public LinkedListCollection() {}

    @Override
    public boolean add(T element) {
        if (size() == 0) {
            head = new Node(element, size());
        }

        getLastNode(head).setNext(new Node(element, size()));
        size  ;
        return false;
    }
}

This gives me the error. "Name clash: The method add(T) of type LinkedListCollection has the same erasure as add(Object) of type CollectionInterface but does not override it"

However, my inherited class declares it with T.

public interface CollectionInterface<T>
{
    boolean add(T element);
}

CodePudding user response:

You have to specify the type to the collection interface too:

public class LinkedListCollection<T> implements CollectionInterface <T>

CodePudding user response:

You forgot the second <T> in

public class LinkedListCollection<T> implements CollectionInterface<T> {
...
}
  • Related