I'm trying to override the following methods but I can't figure out the generics mechanism:
public interface MyInterface<T extends MyGenericObject> {
public void read(long id, Callback<T> callback);
public void readAll(Callback<Collection<T>> callback);
}
public class SomeObject extends MyGenericObject {
...
}
public class MyClass implements MyInterface<SomeObject> {
@Override
public void read(long id, **????** callback) {
...
}
@Override
public void readAll(**????** callback) {
...
}
}
Could you please help me?
EDIT: I had an intermediate Interface between MyClass and MyInterface and the generic SomeObject wasnt passed all the way, that was my problem...
Just replace each T
by SomeObject
which is passed to MyInterface<T>
.
// T has become SomeObject = put everywhere SomeObject instead of T
public class MyClass implements MyInterface<SomeObject> {
@Override
public void read(long id, Callback<SomeObject> callback) {
...
}
@Override
public void readAll(Callback<Collection<SomeObject>> callback) {
...
}
}
EDIT
You have mentioned in comment that it does not compile due of erasure problem (why you getting this error is explained in comment section), however this is not possible with code you have posted:
public interface MyInterface<T extends MyGenericObject> {
void read(long id, Callback<T> callback);
void readAll(Callback<Collection<T>> callback);
}
public interface Callback<T> {}
public class MyGenericObject {}
public class SomeObject extends MyGenericObject {}
public class MyClass implements MyInterface<SomeObject> {
@Override public void read(long id, Callback<SomeObject> callback) {}
@Override public void readAll(Callback<Collection<SomeObject>> callback) {}
}