This is to allow referencing to LinkedList instances as List or Collection.
boolean addSomething(Collection c) {
return c.add(null); // expects collection, with add returning boolean
}
void hackList(LinkedList list) {
addSomething(list); // list is a Collection, OK to pass
}
LinkedList is a List and Collection.
As for why a Collection would need to return boolean, this looks clearly explained in respective javadocs:
...Ensures that this collection contains the specified element (optional operation). Returns true
if this collection changed as a result of the call. (Returns false
if this collection does not permit duplicates and already contains the specified element.)...
public void add(int index, E element)
- I don't see a return type there. – ChrisF♦ May 7 '12 at 21:40add
method with a slightly different signature. Also, why do the signatures differ between these two overloaded functions? – David Faux May 7 '12 at 21:41Set
s, and one is specific toList
s. – user253751 Dec 7 '14 at 2:39