Interface.

S

Stofdeel

Hello,

trying to implement the 'java.util.Collection' interface. Get the following
error.

<ERROR>
MyCollection is not abstract and does not override abstract method
retainAll(java.util.Collection) in java.util.Collection
</ERROR>

<REASON>
"This error is reported when a class implements an interface but does not
supply all the necessary methods or misspells a method name, or has the the
wrong number or types of parameters in one of the interface methods."
</REASON>

But I do implement 'retainAll'...I am missing something here, but I can't
see what. It is not the keyword 'synchronized'. It's not giving the
parameter another name. See code snippet below. I assume the error has
nothing to do with the rest of my code. Any help is appriciated,

thanks.

<MY CODE>
public class MyCollection<E> implements Collection
{
...

public synchronized boolean retainAll(Collection<?> objCollection)
{
boolean blnModified = false;

Iterator<E> objIterator = iterator();
while (objIterator.hasNext())
{
if (!objCollection.contains(objIterator.next()))
{
objIterator.remove();
blnModified = true;
}
}
return blnModified;
}
...
}
</MY CODE>

<JAVA CODE>
public interface Collection<E> extends Iterable<E>
{
// Basic operations
int size();
boolean isEmpty();
boolean contains(Object element);
boolean add(E element); // optional
boolean remove(Object element); // optional
Iterator iterator();

// Bulk operations
boolean containsAll(Collection<?> c);
boolean addAll(Collection<? extends E> c); // optional
boolean removeAll(Collection<?> c); // optional
boolean retainAll(Collection<?> c); // optional
void clear(); // optional

// Array operations
Object[] toArray();
<T> T[] toArray(T[] a);
}
</JAVA CODE>
 
S

Stofdeel

Nevermind, found the error....forgot "<E>" after "implements
Collection"...strange error that produces tho.

Thanks
 
M

Mike Schilling

Stofdeel said:
Nevermind, found the error....forgot "<E>" after "implements
Collection"...strange error that produces tho.

The error is correct: a non-genericized collection (say, one where you
haven't changed the code since 1.4) needs to implement
retainAll(Collection), not retainAll(Collection<?>), and yours did not.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top