Search code examples
javalinked-listtype-mismatch

Can't instantiate a linkedlist of an interface as a linkedlist of a class that implements the interface


I have:

LinkedList<Interface> fred = new LinkedList<Class>();

where Interface and Class are the names of the interfaces and classes respectfully, but java gives me a type mismatch error. My class implements the interface and successfully contains all the functions called for by the interface.

Why does this not work?


Solution

  • You're looking for:

    LinkedList<Interface> fred = new LinkedList<>();
    

    The below is not valid as the type of T must be the same both on the left and right-hand side and clearly, this is not the case.

    LinkedList<Interface> fred = new LinkedList<Class>();