Problem with using generic methods

Hello. I'm having a bit of trouble figuring out how to correctly parameterize a function. Here is some sample code to show what I'm trying to do.

interface SampleInterface<T>{

}

//Emulating a set of SampleInterface

interface OtherSampleInterface<Textends SampleInterface><?>>{

publicvoid add(T myStuff);

}

//Just picking string for the heck of it

class SampleInterfaceImplimplements SampleInterface<String>{

}

publicclass GenericsQuestion<E>{

public <Textends SampleInterface><E>>void myFunc(T i){

//!The method add(capture#1-of ? extends SampleInterface<E>) in the type

//OtherSampleInterface<capture#1-of ? extends SampleInterface><E>> is not applicable for the

//arguments (T)

foo.add(i);

}

OtherSampleInterface<?extends SampleInterface<E>> foo;

}

So, as you can see from the error in the comments of the code, it's saying that there aren't appropriate arguments being given to the add function. However, it seems that <Textends SampleInterface><E>>

would be the same as saying <?extends SampleInterface<E>>

right?If it's not the same, then I can't figure out how to get a proper parameter passed into the function, because I can't do something like add(<? extends SampleInterface<E>).

Any help would be greatly appreciated, as I'm pretty much stuck :).

[2456 byte] By [mr.hwortha] at [2007-11-27 9:58:33]
# 1

> However,

> it seems that <T extends

> SampleInterface<E>>

would be the same as

> saying <? extends SampleInterface<E>>

> right?If it's not the same, then I can't figure

> out how to get a proper parameter passed into the

> function, because I can't do something like add(<?

> extends SampleInterface<E>).

It's not the same. A wildcard signals that you have no idea of what concrete type is used here. Hence, you cannot "add" anything but null. Having a generic parameter T, this gets bound to a known concrete type at compile-time, so you can operate on it.

I am not sure, if you need the wildcard at all, as on getting an element from foo you would have no clue about what type you get. So maybe defining foo as follows suffices your needs:OtherSampleInterface<SampleInterface><T>> foo;

stefan.schulza at 2007-7-13 0:29:13 > top of Java-index,Core,Core APIs...
# 2
Great! That's what I was looking for; thanks for the help.
mr.hwortha at 2007-7-13 0:29:13 > top of Java-index,Core,Core APIs...