I’m trying to set an upper bound to the Result parameter of an AsyncTask like so:
public class MyTask extends AsyncTask<T, Long, V extends Model>
The compiler is complaining that ‘extends is not expected, it’s expecting a comma.
I have tried writing Model as an abstract class and as a regular class.
Any ideas?
Thank you,
David
Since
TandVare unresolved type parameters,MyTaskneeds to be parameterized on them. Try declaring the following:I also changed
callstoclass– I assume that was a typo.In response to your comment:
Here,
MyClassis declaring the type parametersTandV. When type parameters are declared they can optionally be bounded withextends.Tisn’t bounded – it can be any reference type.Vhas an upper bound ofModel. – it must be some type that is or extendsModel.Like any declaration for a class not extending
Object, this is saying thatMyTaskextendsAsyncTask– I’m sure you understand that much.AsyncTaskhas three type parameters: in its declaration they are calledParams,Progress, andResult. Here,MyTaskis providing those type parameters with type arguments –T,Long, andV.So
MyTaskis keepingParamsas an unbounded type parameter, resolvingProgresswith the concrete typeLong, and boundingResultwithModel.See the Java Tutorials for a good introduction to generics. Then see Angelika Langer’s Generics FAQ for further questions.