As a practical example of a general question in a topic, I would like to implement the containsAll method in the Set interface using
public boolean containsAll(Iterable<?> c) { }
I believe this should be allowed since Collection is Iterable , which means that containsAll will cover the interface requirement. Similarly, more generally, the ability to implement interfaces with superclasses of arguments seems to work.
However, Eclipse doesn't say anything (didn't try just javac directly) - can someone explain the reason for this? I am sure that there is something in the specification that does it the way it is, but I would also like to understand the motivation of the requirement. Or am I missing something like Iterable<?> That is not a superclass of Collection<?> ?
As a side question - given that I am declaring two methods, will a method with an Iterable signature always be preferable when calling with the Collection argument?
Eclipse Error:
If I delete the method using the signature of the Collection , leaving Iterable one (see after the error), I get the following:
The type BitPowerSet must implement the inherited abstract method Set<Long>.containsAll(Collection<?>)
Exact implementation:
@Override public boolean containsAll(Collection<?> c) { for (Object o : c) if (!contains(o)) return false; return true; } public boolean containsAll(Iterable<?> c) { for (Object o : c) if (!contains(o)) return false; return true; }
java method-signature interface
Carl
source share