I want to make a List which can hold two kind of Object. What comes in my mind is to use wildcard. Below is my code.
public class Parent {
//code
}
public class ChildOne extends Parent {
//code
}
public class ChildTwo extends Parent {
//code
}
public class Implementation {
List<? extends Parent> list;
public Implementation() {
list.add(new ChildOne());
list.add(new ChildTwo());
ChildOne co = list.get(0);
ChildTwo ct = list.get(1);
}
}
I cannot use <? extends Parent>, since I do add.
But, I cannot use <? super Parent> either, since I do get.
This question stated: don't use a wildcard when you both get and put..
So, how do I do to make a list of several kind of Object without using wildcards?