Hi,
I need to do the following.
Suppose I have a class A and class B and C are subtypes of A.
I want to implement an ArrayList that contains subtypes of A. But I only want to implement this ArrayList using methods in class A.
I was thinking about using the bounded wildcard, but I could not get it to work.
So, I need something like:
public class MyArrayList<? extends A> extends ArrayList<? extends A> { /** * Return a subtype of A */ @Override public <? extends A> get(int index) { // do something with methods only in A. } /** * Add a subtype of A. */ @Override public boolean add(<? extends A> e) { // do something with methods only in A } }
Also I want to override the methods get and add in ArrayList. So when I use MyArrayList, I expect to do the following.
MyArrayList<B> bList = new MyArrayList<B>(); MyArrayList<C> cList = new MyArrayList<C>(); bList.add(new B()); B b = bList.get(0); cList.add(new C()); C c = cList.get(1);
I could not get this to work. Any ideas how this should be done? Or is this not a good design?