I am trying to implement a generic interface in Java with a generic type and am getting compilation errors. I am wondering if the following is possible:
public class ListResponse<T> {
private List<T> response;
public ListResponse(List<T> response) {
this.response = response;
}
public List<T> getResponse() {
return response;
}
}
And the troublesome class:
import java.util.function.Consumer;
import java.util.List;
import com.myorg.response.ListResponse;
public class ListPresenter<T> implements Consumer<ListResponse<T>> {
private ListResponse<T> response;
public <T> void accept(ListResponse<T> response) {
this.response = response;
}
// Rest of class
}
I was hoping to then have calling code like this:
ListPresenter<Integer> presenter = new ListPresenter<>();
However I get a compilation error with the following message:
error: ListPresenter is not abstract and does not override abstract
method accept(ListResponse<T>) in Consumer [ERROR] where T is a
type-variable: [ERROR] T extends Object declared in class
ListPresenter
public <T> void accept(ListResponse<T> response)should bepublic void accept(ListResponse<T> response)(first<T>removed)