Consider this example (typical in OOP books):
I have an Animal
class, where each Animal
can have many friends.
And subclasses like
As you said passing a class would be OK, you could write this:
public T callFriend(String name, Class clazz) {
return (T) friends.get(name);
}
And then use it like this:
jerry.callFriend("spike", Dog.class).bark();
jerry.callFriend("quacker", Duck.class).quack();
Not perfect, but this is pretty much as far as you get with Java generics. There is a way to implement Typesafe Heterogenous Containers (THC) using Super Type Tokens, but that has its own problems again.