I'm working with Google Web Toolkit, and I'm having problems implementing a generic interface. I'm not really familiar with generics, doing an upgrade on someone else's code here.
Here's what I want to do: I want to have an implementation of a generic callback interface that does some logging, and then subclass that implementation in order to handle specific callback scenarios.
The interface is something like this:
public interface AsyncCallback<T> {
void MethodFromAsyncCallback(T result);
}
The abstract and concrete implementations look something like this:
class CallbackBase implements AsyncCallback<Object> {
public abstract void doStuff(Object result);
public void MethodFromAsyncCallback(Object result) {
// IMPORTANT STUFF
// here are things I would like to do for all callbacks, hence the superclass.
// Then we do the subclass specific things.
doStuff(result);
}
}
class SpecificCallback extends CallbackBase
{
public void doStuff(Object result) {
Integer i = (Integer)result;
// do stuff with i
}
}
The callbacks are required to be fired from
public interface MyServiceAsync {
public void DoSomeThing(AsyncCallback<Integer>);
}
And then it all comes together in a call that looks like this:
MyServiceAsync myService = (MyServiceAsync)GWT.create(MyServiceAsync.class);
myService.DoSomeThing(new SpecificCallback());
And here's where we have a problem!
When the GWT.create()
implements the interface I created, it demands that the type given to AsyncCallback
is specified (matches a type elsewhere, outside the scope of this question), hence making DoSomething(AsyncCallback<Integer>)
an Integer rather than an Object. This is beyond my control.
It complains that DoSomething()
takes AsyncCallback<Integer>
. I'm giving it something that inherits from something that is an AsyncCallback<Object>
. I guess with generics, concepts of inheritance get somewhat broken?
So my question is this:
Either how can I mush this together so that DoSomething()
will recognize that that SpecificCallback
meets it's requirements,
or how can I structure the relationship between CallbackBase
and SpecificCallback
so that duplicate code is avoided, but SpecificCallback
implements AsyncCallback<Integer>
directly?
Thanks.