I have boolean addAll(int index, Collection coll) this method in list interface.
ID: 3913014 • Letter: I
Question
I have boolean addAll(int index, Collection coll) this method in list interface. And now I have AbstractList which implements List interface and ArrayList which extends AbstractList. Then the method addAll should be implemented in which class? The abstractList or ArrayList and why?
Javadoc for addAll method is this --Inserts all the elements of <tt>coll</tt> into this collection at
* the specified location. If both this collection and the parameter
* are the same collection, then the operational behavior is undefined
* (i.e. bad things can happen).
* @param index the location at which to insert
* @param coll the collection from which to draw elements for addition.
* @return true when this collection is modified as a result.
public boolean addAll(int index, Collection<? extends E> coll)
Explanation / Answer
ArrayList extends AbstractList, which is an abstract class. In practice, addAll() method that you mentioned is implemented in AbstractList class, but is overridden by ArrayList class. That means the implementation is written in both the classes. Follow the links attached below to verify the claim. This is because AbstractList provides a very basic skeleton of addAll() for many types of lists to follow, while ArrayList has the very specifically written addAll() method for efficient use inside ArrayList Class.
AbstractList : http://developer.classpath.org/doc/java/util/AbstractList-source.html [line 179-186]
ArrayList : http://developer.classpath.org/doc/java/util/ArrayList-source.html [line 424-440]
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.