I have this method for retrieving objects that are instances of this class:
public class UtilitiesClass {
public static final Collection<Animal> get(Collection<Animal> animals, Class<? extends Animal> clazz) {
}
...
}
To call the method, I can do something like this:
Collection<Animal> dogs = UtilitiesClass.get(animals, Dog.class);
This is good, but I would also like to be able to call the method in two ways:
Collection<Animal> dogs = UtilitiesClass.get(animals, Dog.class);
or
Collection<Dog> dogsTyped = UtilitiesClass.get(animals, Dog.class);
I mean, I want to save the result of the method in the Dog’s collection or in Animal, because Dog.classextendsAnimal.class
I thought something like this:
public static final <T> Collection<T> get(Class<T extends Animal> clazz) {
}
But that will not work. Any hint?
Edit: Finally, using @Rohit Jain's answer, this is the solution when you call the UtilitiesClass method:
Collection<? extends Animal> dogsAnimals = UtilitiesClass.get(animals, Dog.class);
Collection<Dog> dogs = UtilitiesClass.get(animals, Dog.class);