I wrote some generics with inheritance, but cannot get it to work typesafe. My goal is to provide a general BaseService
on that I can exection the action(base)
method, no matter if the base
object is a Foo extends Base
or Bar extends Base
.
The following does not work, why?
class Base;
class Foo extends Base;
interface BaseService<T extends Base> {
void action(T base);
}
class FooService implements BaseService<Foo> {
void action(Foo foo) {
}
}
//usage:
//complains that the method is not applicable for the argument,
//and that I should change action(T) to action(Base). Why?
Base base;
getService().action(base);
//just to demonstrate the problem
BaseService<? extends Base> getService() {
return new FooService();
}