I’m having a problem properly implementing generics in abstract classes that need to be overridden.
public abstract class AbstractSerachView {
DataStore<AbstractCriteria, AbstractResults, AbstractService> resultsStore;
public abstract DataStore<AbstractCriteria, AbstractResults, AbstractService> getResultsStore();
}
the child clases is as follows:
public class TravelSearchView extends AbstractSearchView {
DataStore<TravelCriteria, TravelResults, TravelService> resultsStore;
public DataStore<TravelCriteria, TravelResults, TravelService> getResultsStore() {
return resultsStore;
}
}
This sort of abstraction does not work at all, although it would be greatly appreciated if I could get this work in some way. What is the correct approach here? I have very little experience with generic types.
The problem is that Eclipse indicates an error in the child class: The return type is incompatible with AbstractSearchView.getResultsStore()
Furthermore, if I leave the child class get method with the 3 abstract types, and override the variable with the 3 travel types, everything is okay except for the return line, where Eclipse indicates: Type mismatch: Cannot convert from DataStore to DataStore
Here are your alternatives:
a) Change the superclass type signature to
b) Add the type parameters to the class as a whole:
The issue is that
DataStore<TravelCriteria, TravelResults, TravelService>is not a subtype ofDataStore<AbstractCriteria, AbstractResults, AbstractService>, even if those generic type arguments are respectively subtypes.