1

I have question about Java Generic WildCard

Why this code is not working?

If B extends A,

"? extends A" means that A and B is allowed for "?"
"? supers B" means that B and A is allowed for "?".

Am I wrong?

enter image description here

Martin Kim
  • 47
  • 3

1 Answers1

0

Its because it is said that list hold some type X that extends Animal. Notice that it is not anything that is animali but rather something, but one thing that is animal. So you know for sure that there are only instances of X animals in the list. Since you don't know what that X is, you cannot add any animals to the list as it might not be an X.

Antoniossss
  • 31,590
  • 6
  • 57
  • 99