Why isn\'t Collection.remove(Object o) generic?
Seems like Collection
could have boolean remove(E o);
Then, when you ac
Assume one has a collection of Cat
, and some object references of types Animal
, Cat
, SiameseCat
, and Dog
. Asking the collection whether it contains the object referred to by the Cat
or SiameseCat
reference seems reasonable. Asking whether it contains the object referred to by the Animal
reference may seem dodgy, but it's still perfectly reasonable. The object in question might, after all, be a Cat
, and might appear in the collection.
Further, even if the object happens to be something other than a Cat
, there's no problem saying whether it appears in the collection--simply answer "no, it doesn't". A "lookup-style" collection of some type should be able to meaningfully accept reference of any supertype and determine whether the object exists within the collection. If the passed-in object reference is of an unrelated type, there's no way the collection could possibly contain it, so the query is in some sense not meaningful (it will always answer "no"). Nonetheless, since there isn't any way to restrict parameters to being subtypes or supertypes, it's most practical to simply accept any type and answer "no" for any objects whose type is unrelated to that of the collection.