Adam Richards wrote:The problem is that the line return dao.getResultModel() generates a compile error "Type mismatch: cannot convert from MultiFacilitySearchModel to T". I don't understand why I'm getting this error, since MultiFacilitySearchModel implements the three interfaces specified for the generic type T.
No it doesn't, unless it
also includes a T in its definition (which, on the surface, would seem kind of pointless; and which doesn't appear to be the case anyway).
What am I missing?
An
English explanation of exactly what you're trying to achieve.
One thing that's probably worth remembering about generics is that it's a
kludge - albeit a pretty good one - to mask the fact that compile-time type checking wasn't really properly implemented in
Java from the outset. And for that reason there are some things that it
just can't do.
Even constructs as "simple" as:
public interface MyInterface<T, C extends MyInterface<T, C>>
(which I notice is sprouting up a lot in version 8 as a form of "factoried" return type) have to be used with caution.
And for that reason, the general rule is "keep it simple". If I can't read your class definition and understand
immediately what's going on, the chances are you're going to run into problems down the line.
Generics, IMO, is an "80/20" solution. It does the basics well for very little effort, but it can't do everything; and the more you try to make it a "compiler for the compiler", the more problems you're likely to run into.
My 2¢.
Winston