I have a question about the use of generic types in the Java Collections framework.
Here's a snippet of the Set
interface taken from Oracle's Java Collections thread (found here):
public interface Set<E> extends Collection<E> {
// Basic operations
int size();
boolean isEmpty();
boolean contains(Object element);
// optional
boolean add(E element);
// optional
boolean remove(Object element);
...
My question is this: Given that Set<E>
is generic with generic type parameter E
, why are the contains
and remove
methods declared to take arguments of type Object
? The add
method takes an argument of type E
, why don't contains
and remove
do the same?