I'm having a hard time trying to figure this out. Say I have the following code:
class Animal { }
class Mammal extends Animal { }
class Giraffe extends Mammal { }
...
public static List<? extends Mammal> getMammals() { return ...; }
...
public static void main(String[] args) {
List<Mammal> mammals = getMammals(); // compilation error
}
Why does the assignment result in a compilation error? The error is something like:
Type mismatch: cannot convert from List<capture#4-of ? extends Mammal> to List<Mammal>
According to my understanding of covariance, the getMammals() method returns a list that will always contain Mammal objects so it should be assignable. What am I missing? Thanks!