I have this method to retrieve the objects which are instance of a given class:
public class UtilitiesClass {
public static final Collection<Animal> get(Collection<Animal> animals, Class<? extends Animal> clazz) {
// returns the Animals which are an instanceof clazz in animals
}
...
}
To call the method, I can do something like this:
Collection<Animal> dogs = UtilitiesClass.get(animals, Dog.class);
That is good, but I would also like to be able to call the method in these two ways:
Collection<Animal> dogs = UtilitiesClass.get(animals, Dog.class);
or
Collection<Dog> dogsTyped = UtilitiesClass.get(animals, Dog.class);
What I mean is that I want to be able to store result of the method in a Dog Collection or in an Animal one, because Dog.class
extends Animal.class
I was thinking in something like this:
public static final <T> Collection<T> get(Class<T extends Animal> clazz) {
// returns the Animals which are an instanceof clazz
}
But it does not work. Any hint?
Edit: Finally, using @Rohit Jain answer, this is the solution when you call to the UtilitiesClass method:
Collection<? extends Animal> dogsAnimals = UtilitiesClass.get(animals, Dog.class);
Collection<Dog> dogs = UtilitiesClass.get(animals, Dog.class);