I am a bit confused with this, so a bit of clarification would be appreciated.
public <T extends Animal> void addAll(List<T> animals)
vs.
public void addAll(List<Animal> animals)
The difference is in which type parameters in the List will be accepted by the method.
In the first method, T can be Animal or any subclass, so addAll will accept a List<Animal>, a List<Dog>, or a List<Cat>. Note that this signature is equivalent to
public void addAll(List<? extends Animal> animals)
when you don't need the exact type of Animal in the body of the method.
In the second method, you've specified that the type parameter must be Animal. Java's generics are invariant, so no subtypes of Animal will be allowed. This method will accept a List<Animal>, but not a List<Dog> or a List<Cat>.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With