Reputation: 9819
I am trying to implement a generic callable to delegate the modification of different types of accounts. The code looks like this:
import java.util.concurrent.Callable;
class Task<T extends BaseAccount> extends Callable<T extends BaseAccount> {
private final T t;
public Task(T t) {
this.t = t;
}
@Override
public T call() throws Exception {
t.increment();
return t;
}
}
The BaseAccount is just an abstract class as follows:
abstract class BaseAccount {
abstract public void increment();
}
But obviously I am not getting there as I see a lot of generics-related compilation errors. Would appreciate help through this.
Upvotes: 0
Views: 7494
Reputation: 178293
First, in a class, you don't extend an interface, you implement it. Second, you don't need to repeat that T
extends BaseAccount
in the Callable
part. Just do this:
class task<T extends BaseAccount> implements Callable<T>
EDIT
The <T extends BaseAccount>
in the task
class declaration is where you have declared your generic type parameter T
, so a bounds restriction is appropriate here. But implements Callable<T>
is just a reference to the type parameter T
, just like everywhere else in the class where you use T
-- the instance variable declaration, the constructor and the method.
Additionally, it's conventional for java class names to start with a capital letter, e.g. Task
, but that's not important for resolving the compiler errors.
Upvotes: 6