Search code examples
javagenericsinterfaceoverriding

How to override an interface that uses a generic method?


I do not have access to the interface's code; I just know there is a method named dequeue() to be overridden in the abstract class which implements it.

The name of the interface is UrgencyQueue, and the abstract class is BaseLinkedUrgencyList.

below is all the info i have on the interface, a UML pic and javadoc

enter image description hereenter image description here

Below is my attempt to override said method:

public <Type> void dequeue(Consumer<Type> action) {
    throw new UnsupportedOperationException("Method: not implemented.");
}

The declaration of the class includes implements ''interface''.

The error produced is:

error: name clash: dequeue(Consumer<Type#1>) in BaseLinkedUrgencyQueue and dequeue(Consumer<Type#2>) in UrgencyQueue have the same erasure, yet neither overrides the other
    public <Type> void dequeue(Consumer<Type> action) {
                       ^
  where Type#1,Type#2 are type-variables:
    Type#1 extends Object declared in method <Type#1>dequeue(Consumer<Type#1>)
    Type#2 extends Object declared in class BaseLinkedUrgencyQueue

Solution

  • It looks like the dequeue specified in the interface is supposed to use the generic type parameter of the class, not of the method.

    So if the class declaration looks something like this:

    public class BaseLinkedUrgencyList<Type> implements UrgencyQueue<Type>
    

    then the method implementation should be like this:

    @Override
    public void dequeue(Consumer<Type> action)