What is the difference between public <T extends Animal> void addAll (List <T> animals) and public void addAll (List <Animal> animals)?

I am a little confused by this, so a little clarification will be appreciated.

public <T extends Animal> void addAll(List<T> animals)

vs.

public void addAll(List<Animal> animals)
+4
source share
2 answers

The difference is what types of parameters Listwill be accepted by the method.

In the first method, there Tcan be Animaleither any subclass, therefore it addAllwill accept List<Animal>, a List<Dog>or List<Cat>. Note that this signature is equivalent

public void addAll(List<? extends Animal> animals)

when you don’t need the exact type Animalin the body of the method.

, Animal. Java- , Animal . List<Animal>, List<Dog> List<Cat>.

+10

"T" - . , List T , List of type Animal Animal, .

Generics.

0

Source: https://habr.com/ru/post/1626067/


All Articles