"VisionSet" <spam@ntlworld.com> wrote in news:N7hpf.47909$uR.8369@newsfe7-
gui.ntli.net:
> I have an attribute:
>
[quoted text clipped - 11 lines]
> --
> Mike W
List<ConcreteType> is not a subclass of List<IntfType>. In fact, there is
no relation between those two types, even though the container is the same
and the parameters are related types. So, I don't think it's possible.
Maybe there is a workaround, although I don't know one off the top of my
head. Maybe if you give a concrete example of what you want to do someone
can suggest how to get it done.

Signature
Beware the False Authority Syndrome
Paul Bilnoski - 19 Dec 2005 16:25 GMT
> "VisionSet" <spam@ntlworld.com> wrote in news:N7hpf.47909$uR.8369@newsfe7-
> gui.ntli.net:
[quoted text clipped - 21 lines]
> head. Maybe if you give a concrete example of what you want to do someone
> can suggest how to get it done.
They do have a relationship, the two containers are covariant types. You
can use covariant notation, or what java calls "capture" (the ? notation
in parameter expressions) to give you a list of as much as you can tell
of the items in the list.
If you get back a list of '? extends A' it's as good as a List<A> in
that you can do whatever you could to an A, but you also know that what
is in the list is not only instance of the base type A.
import java.util.List;
import java.util.ArrayList;
public class GT
{
public static interface A { }
public static class B implements A { public String toString() {
return "B"; } }
public static List<B> bees;
public static List<? extends A> asListOfA() { return bees; }
public static void main(String[] args)
{
bees = new ArrayList<B>();
bees.add(new B());
bees.add(new B());
List<? extends A> as = asListOfA();
for (A a : as)
System.out.println(a.toString());
}
}
--Paul Bilnoski
> I have an attribute:
>
[quoted text clipped - 5 lines]
>
> where ConcreteType implements IntfType
If you could assign from List<ConcreteType> to List<IntfType>, additions
to the later could break the former reference. For instance:
List<String> s = new ArrayList<String>();
List<Object> o = s; // Does not work!
o.add(new char[4]);
String c = s.get(0); // What happens here???
So, either start with List<IntfType>, use List<? extends IntfType> or
List<T extends IntfType> within a generic method, constructor or instance.
Tom Hawtin

Signature
Unemployed English Java programmer
http://jroller.com/page/tackline/
ricky.clarkson@gmail.com - 19 Dec 2005 01:00 GMT
Make the attribute List<IntfType>. Simple.
One line replies suck, so this is the second line.
VisionSet - 19 Dec 2005 16:20 GMT
> > I have an attribute:
> >
[quoted text clipped - 16 lines]
> So, either start with List<IntfType>, use List<? extends IntfType> or
> List<T extends IntfType> within a generic method, constructor or instance.
Yes of course.
I was trying to avoid making a new List object but I've done this which is
still nice and clean:
private LinkedList<ConcreteType> attr;
public List<IntfType> getMyTypes() {
return new ArrayList<List>(attr);
}
--
Mike W