I’ve been trying to extend the ArrayList class without much success. I want to extend it, and be able to parameterize it.
So normally you have something like
ArrayList<SomeObject> list = new ArrayList<SomeObject>();
I want
MyList<SomeObject> list = new MyList<SomeObject>();
Simply extending ArrayList doesn’t work.
public class MyList extends ArrayList ...
The when I try to use it I get the error
The type MyList is not generic; it cannot be parameterized with arguments <SomeObject>
I’ve tried variations of
public class MyList extends ArrayList<Object> public class MyList<SubObject> extends ArrayList<Object>
with no success, If I use the subobject behind the class name it appears to work, but hides methods in the subobject class for some reason.
Any thoughts or suggestions on how to get this working right are appreciated.
You need to specify a type for the
ArrayList‘s type parameter. For generic type parameters,Tis fairly common. Since the compiler doesn’t know what aTis, you need to add a type parameter toMyListthat can have the type passed in. Thus, you get:Additionally, you may want to consider implementing
Listand delegating to anArrayList, rather than inheriting fromArrayList. ‘Favor object composition over class inheritance. [Design Patterns pg. 20]’