Skip to Main Content

Java Programming

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!

Extending ArrayList with generics

807589Dec 22 2008 — edited Dec 22 2008
Using Java 1.4 I had extended ArrayList to disallow null entries being added to the list. All I had to do was override one of the constructors and the add methods to check for null entries in the collection and/or null parameters.

With ArrayList in 1.5 using generics I'd like to take advantage of this. I've changed my extends for the class to
public class MyArrayList<E extends ArrayList<E>>
But the constructor with the collection parameter, add and addAll methods give an error.
public MyArrayList(final Collection<? extends E> c) {
  super(c);
  checkCollection(c);
}
public boolean add(final E obj) {
  ProgUtils.checkParameterNull("obj", obj);
  return super.add(obj);
}
The compiler give 'The constructor Object(Collection<capture#4-of ? extends E>) is undefined' and 'The method add(E) is undefined for the type Object'.

Can anyone point me to the appropriate tutorial/blog that explains how to extend a class that uses generics?
Thanks
Lori <*>
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jan 19 2009
Added on Dec 22 2008
7 comments
458 views