Re: Generic curious
On Thu, 23 Apr 2009, Zibi wrote:
Can anyone explain my difference between definition two methods:
1.
public <T extends JxbAbstractDomainObject> T
getNextXmlObjectFromSet(Set<Class<T>> pmSet)
{
return null;
}
2.
public <T extends JxbAbstractDomainObject> T
getNextXmlObjectFromSet(Set<Class<? extends JxbAbstractDomainObject>> lvSet)
{
return null;
}
The call:
Set<Class<? extends JxbAbstractDomainObject>> lvSet = new HashSet<Class<?
extends JxbAbstractDomainObject>>();
lvSet.add(JxbCl1.class);
lvSet.add(JxbCl2.class);
System.out.println( lvManager.getNextXmlObjectFromSet(lvSet));
and first method is not compatibile with call, why ?
I was thinging that it is the same.
'? extends T' does *not* mean the same as 'T'. T means T, but ? extends T
means 'some class S, whose exact identity we don't know here, which
extends T'. For instance, if you had a List<? extends InputStream>, that
could really be a List<FileInputStream>, a List<DataInputStream>, etc.
The result is that you can only use objects bound to ? types in certain
ways. For instance:
List<? extends InputStream> streams;
InputStream stream = streams.get(0);
That's okay, because whatever ? really is, the things in the list must at
least be InputStreams.
But this:
InputStream stream;
List<? extends InputStream> streams;
streams.add(stream);
Is *not* okay, because you can't know whether the stream is the right kind
of stream to go in the list.
In your case, Set<Class<? extends JxbAbstractDomainObject>> means a set of
class objects, all of which represent subtypes of some specific but
unknown type, which is itself a subtype of JxbAbstractDomainObject.
tom
--
unconstrained by any considerations of humanity or decency