1

I have a java assignment where i am supposed to construct a class that works like a Multiset. The class has to implement the interface Collections. I have tried to do this and declare all the methods that are in Collections (found here). But when i compile this code I get the following error:

error: TreeMultisetNy is not abstract and does not override abstract method retainAll(Collection<?>) in Collection

Why is this happening?

Heres my code:

import java.util.*;

public class TreeMultisetNy<E extends Comparable<E>> implements Collection<E> {
private Map<E, Integer> data = new TreeMap<E, Integer > ();

public boolean add(E ny) { 
    return true;
}
public boolean addAll(Collection<? extends E> c){
    return false;
}
public void clear() {

}
public boolean contains(E what) {
    return false;
}
public boolean containsAll(Collection<?> c) {
    return false;
}
public boolean equals(E what) {
    return false;
}
public int hashCode() {
    return 0;
}
public boolean isEmpty() {
    return false;
}
public Iterator<E> iterator() {
    return null;
}
public boolean remove(E what) {
    return false;
}
public boolean removeAll(Collection<?> c) {
    return false;
}
public boolean retainAll(Collection<?> c) {
    return false;
}
public int size() {
    return 0;
}
public Object[] toArray() {
    return null;
}
public Object[] toArray(Object[] a){
    return null; 
}
}

I found this question: How to create a class that implements java.util.collections But I dont believe I am making the same mistake as that guy, or am i wrong?

Please give me some hints, i have been coding php for years but object orientation is new to me!

7
  • 2
    Try using the method signature public boolean retainAll(Collection<?> c) instead of public boolean retainAll(Collection<E> c). Commented Apr 7, 2013 at 16:51
  • and public boolean addAll(Collection<? extends E> c) . Also hava a look at AbstractCollection - extending that instead of implementing the whole interface yourself may save some work. Commented Apr 7, 2013 at 16:54
  • What they mean by 'is not abstract' is that because your class is not abstract, it has to implement all of the methods. Commented Apr 7, 2013 at 16:55
  • 2
    You may be able to reduce the amount of work by extending java.util.AbstractCollection. You still have to implement a few methods, but not as many as you are doing. Commented Apr 7, 2013 at 16:57
  • Thanks @TedHopp that saved me a couple of errors! Commented Apr 7, 2013 at 17:05

1 Answer 1

1

The correct signatures for the "All" methods are as follows:

boolean addAll(Collection<? extends E> c)
boolean containsAll(Collection<?> c)
boolean removeAll(Collection<?> c)
boolean retainAll(Collection<?> c)

You need to change your class accordingly.

Also, the signature of the one-arg version of toArray() is wrong. It should be

public <T> T[] toArray(T[] a)

There are other errors the same lines. You need to carefully go through your class, making sure each method has exactly the correct signature.

See Javadoc for details.

Sign up to request clarification or add additional context in comments.

5 Comments

Thank you, that got rid of all the errors except for the first one :)
@user2254899: Which one is the first?
"error: TreeMultisetNy is not abstract and does not override abstract method retainAll(Collection<?>) in Collection"
@user2254899: Well, does it override retainAll(Collection<?>), with this exact signature?
I'm sorry but I'm not following you. I have now updated the question with my current code and the remaining error

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.