Java covariance -


I am trying to find out. Say I have the following code:

  class animal {} class mammal animal {} spreads in class giraffe spreads mammals {} ... public static list & lt;? Mammal & gt; Extends Millmills () {Return ...; } ... public static zero chief (string [] args) {list & lt; Mammal & gt; Mammals = matching mammals (); // compilation error}  

Why is the result of the assignment in a compilation error? The error is something like this:

  type does not match: can not be changed from the list & lt; Capture # 4-Why? Mammal & gt; For the expanded list & lt; Mammal & gt;  

According to my understanding of the sect, getMammals () method gives a list which is always mammals Objects should be assigned to me, what am I missing?

Because getMammals can return a list & lt; Giraffe & gt; , and if that list & lt; Mammal & gt; , you can add it to zebra adding zebra to a list of giraffe can not be allowed Is that what you can do?

  in the category of class zebra mammals {} & lt; Giraffe & gt; Giraffe = new list & gt; Giraffe & gt; (); & Lt; Mammal & gt; Mammals = giraffes; // Not allowed mammals.ed (new zebra ()); // will add a zebra to the list of giraffe  

Comments

Popular posts from this blog

c# - ListView onScroll event -

PHP - get image from byte array -

Linux Terminal Problem with Non-Canonical Terminal I/O app -