Skip to Main Content

Java APIs

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Collections<Item>,and Item<Collection>, how can I solve this?

843793May 26 2006 — edited May 28 2006
Hi,

I have a class which functions as a collection where it has an add and remove of special items,
interface MyCollection<T extends Item>{
public void add(T t);
public void remove(T t);
}
This special collection will always have Items: These special items must have a reference to their specific collector of same type Items
interface Item{
public String getName();
public MyCollection<?> getCollection(); //the problem is here
Now, when I extend both classes, SpecialCollection will have SpecialItems:
public class SpecialCollection extends MyCollection<SpecialItem>{
}
//and
public class SpecialItem extends Item{

}

///to Test:
public static void main(String []a){
SpecialCollection sc=....
sc.add(new SpeicalItem());
SpecialItem si=sc.getItem();

MyCollection<SpecialItem> sc2=si.getCollection();//Error because I it will return MyCollection<?>
}
How can I slove this problem without a case?!
in other words, How can parametrize a method list this:
interface Item{
public MyCollection<T super THIS_CLASS WEATHER IT ITEM OR SUBTYPE CLASS> getCollection();
}

thank you!
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jun 25 2006
Added on May 26 2006
3 comments
170 views