Using Generics to execute implemented functions
Hi,
I'm trying to use generics to take an object and then execute the object using returned function.
I've simplified it below, but the gist is there. The "stuff.doStuff(obj)" call causes a compile time error that basically says that obj needs to be of type <? extends Object> instead of Object.
Am I fundamentally wrong here? Is there a way to cast it?
Thanks for taking a look.
public class Main {
public void notMain(Object obj) {
Stuffer<? extends Object> stuff = getStuffer(obj.getClass());
stuff.doStuff(obj);
}
private <T> Stuffer<T> getStuffer(Class<T> clazz) {
// retrieve instance based on class
}
private interface Stuffer<T> {
public void doStuff(T stuffToDo);
}
}