John Smith
John Smith

Reputation: 111

How do I do generic generics?

import java.util.LinkedList;
class GenericInseption{
  public static void main(String[] args){
    LinkedList<LinkedList<Dad>> listOfLists = new LinkedList<>();
    LinkedList<Dad> list = new LinkedList<>();
    listOfLists.add(list);
    test(listOfLists);//error
  }
  private static void test(LinkedList<LinkedList<? extends Person>> listOfLists){
    for(LinkedList<? extends Person> list : listOfLists){
      for(Person person : list){
        //peform operation on person objects
      }
    }
  }
}
class Person{}
class Dad extends Person{}

error

GenericInseption.java:7: error: method test in class GenericInseption cannot be applied to given types;
    test(listOfLists);//error
    ^
  required: LinkedList<LinkedList<? extends Person>>
  found: LinkedList<LinkedList<Dad>>
  reason: argument mismatch; LinkedList<LinkedList<Dad>> cannot be converted to LinkedList<LinkedList<? extends Person>>
1 error

but LinkedList< Dad> is a subtype of LinkedList< ? extends Person>

How is this supposed to work?

Upvotes: 2

Views: 48

Answers (1)

rgettman
rgettman

Reputation: 178303

Even though LinkedList<Dad> is a subtype of LinkedList<? extends Person>, a LinkedList<LinkedList<Dad>> is not a subtype of LinkedList<LinkedList<? extends Person>>, for the same reason that a List<Dog> is not a subtype of List<Animal> -- Java's generics are invariant. In this case, they are invariant at each level, not just at the ? extends Person level but at the LinkedList<? extends Person> level.

Add another wildcard in front of LinkedList in the first level of generics in the test method.

//                                  vvvvvvvvv
private static void test(LinkedList<? extends LinkedList<? extends Person>> listOfLists){

Or, to make it more broadly applicable, use List instead of LinkedList:

private static void test(List<? extends List<? extends Person>> listOfLists){
    for(List<? extends Person> list : listOfLists){

Upvotes: 6

Related Questions