Nested Generic Types & Bounding
Hi,
Why does:
ArrayList<List><? super Integer>> o = new ArrayList<List><Integer>>();
not compile?
I have some classes all of which extend Column<Context><Item>> (where Column is an interface, Context is a concrete class, and Item (and there's some which use SubItem) is a concrete class. I would like to be able to put them into a List but I cannot create a List of the correct type (the problem I believe arises from my compile error above).
Anyone know why this is the case?
Thanks
Jonathan
[587 byte] By [
j_p_ga] at [2007-11-27 1:35:07]

I didn't look too closely to that Integer example of yours because I'm not sure if that's possible since Integer is immutable (a final class).
But the following works fine for me:
import java.util.*;
public class Main {
public static void main(String[] args) {
List<? super Column<Context<Item>>> list = new ArrayList<Column<Context<Item>>>();
list.add(new ColumnImpl<Context<Item>>());
}
}
interface Column<C> { /* ... */ }
class ColumnImpl<C> implements Column<C> { C context; }
class Context<I> { I item;}
class Item{ /* ... */ }
I now see you're trying to store a List in a List. So perhaps this is what you're after:import java.util.*;
public class Main {
public static void main(String[] args) {
List<? super Column<Context<Item>>> list =
new ArrayList<Column<Context<Item>>>();
List<List<? super Column<Context<Item>>>> listOfLists =
new ArrayList<List<? super Column<Context<Item>>>>();
list.add(new ColumnImpl<Context<Item>>());
listOfLists.add(list);
}
}
interface Column<C> { /* ... */ }
class ColumnImpl<C> implements Column<C> { C context; }
class Context<I> { I item;}
class Item{ /* ... */ }
B.t.w., there is an excellent Generics section in this forum.
http://forum.java.sun.com/forum.jspa?forumID=316
Some very skilled generics-people post there. I'm thinking of stefan.schulz and Peter Ahe, who is one of the designers of Sun's generics implementation. So you might want to go there if my answer was insufficient.
Good luck.
This is close, but I want to be able to use a SubItem, eg:
public class Main
{
public static void main(String[] args) {
List<? super Column<Context<? super SubItem>>> list = new ArrayList<Column<Context<? super SubItem>>>();
list.add(new ColumnImpl<Context<Item>>());
list.add(new ColumnImpl<Context<SubItem>>());
}
}
interface Column<C> { /* ... */ }
class ColumnImpl<C> implements Column<C> { C context; }
class Context<I> { I item;}
class Item{ /* ... */ }
class SubItem extends Item {}
Unforturnately this fails to compile when I add either object to the list?
> ...> Unforturnately this fails to compile when I add> either object to the list?Hmmm, not sure. I suggest posting this in the generics forum. If you do so, please post a link to your new thread in this one so people know you've moved the discussion
Ok thanks, I've posted in the generics forum, although with slightly different class names to more closely reflect my problem. http://forum.java.sun.com/thread.jspa?threadID=5162649
> Ok thanks, I've posted in the generics forum,
> although with slightly different class names to more
> closely reflect my problem.
> http://forum.java.sun.com/thread.jspa?threadID=5162649
No problem. I'll monitor the new thread as well since I am curious about the solution too.
Good luck.