i have these classes
class SuperParent {
@Override
public String toString() {
return "SuperParent";
}
}
class A extends SuperParent {
@Override
public String toString() {
return "A";
}
}
class B extends SuperParent {
@Override
public String toString() {
return "B";
}
}
and i want to have a List of ArrayLists , so i can add to ArrayList of B’s and A’s
i tried
List<ArrayList<?extends SuperParent>> mList = new ArrayList<ArrayList<SuperParent>>();
but does not compile
when i tried this,
ArrayList<ArrayList<SuperParent>> mList = new ArrayList<SuperParent>>();
it compiles but i cant add ArrayLists of As or ArrayLists Bs
i understand java types and inheritance too but when it comes to Generics i miss with it .
Any reference or a declaration would help me .
This is how you would declare a list of lists where the elements of outer list can be a list of A or list of B:
Generally, it is recommended to use interface types than concrete types when declaring or accepting as method parameters. That way, the above snippet would look like below: