Maybe something like this:
public static <T> T getItem(Element element, Class<T> clazz) {
IParsable item = null;
if (clazz == IFoo.class) {
item = new Foo();
} else if (clazz == IBar.class) {
item = new Bar();
}
item.parse();
return (T) item;
}
The fact that Foo
is an IParsable
is checked at compile time. The other cast (to (T)
) is still a runtime exercise - but it was also the case in your example.