0
package Practice;

import java.util.LinkedList;
import java.util.Iterator;

public class CollectionTest {

public static void main(String[] args) {

    LinkedList<String> c = new LinkedList<String>();
    c.add("Hello");
    c.add("World");
    c.add("java");

    Iterator<String> it = c.iterator();
    while (it.hasNext()) {
        System.out.println(it.next());
    }

}
}

My question: Since Collection extends Iterable interface, so it has the abstract method iterator() which is defined in Iterable. So, LinkedList which is a concrete class that implements List interface that extends Collection interface, should have an overridden version of the iterator() method, I guess...

I saw the SUN company source codes for the ArrayList class, and inside there is an inner class "Itr" which implements Iterator interface, and by calling the "public Iterator iterator()" method inside ArrayList class, it will return a "new Itr()".

But my confusion is that I cannot find an iterator() method in the LinkedList. I only found a "descendingIterator()" method in LinkedList class which is not what I am looking for... But where exactly is the overridden version of the iterator() method in LinkedList? In my codes I created a LinkedList object, and used this object to invoke its iterator() method, and it works. But since there's no overridden version of interator() method in LinkedList, why can it work? Is it calling the interator() method in its super classes like List? Collection? But these are all interfaces with no concrete method body for interator().... Also why it still works when iterator reference "it" is calling the hasNext and next methods?

Sorry spare with my lengthy question statements... I hope this can make you understand my confusions.... Appreciate greatly for your help thank you!

3
  • 2
    Comes from public abstract class AbstractSequentialList<E> extends AbstractList<E>. In Eclipse you can right click the method and choose "Open Declaration" and it shows you where the method is. Commented Jul 19, 2018 at 15:25
  • Thank you guys so much!!! I am clear now. Commented Jul 19, 2018 at 15:30
  • Simillar question resolved here Commented Nov 19, 2019 at 12:45

3 Answers 3

4

LinkedList extends an abstract AbstractSequentialList class which has the definition of methods get, set , iterator, add & remove. Because AbstractSequentialList extends a abstract AbstractList class.

public class LinkedList<E>
extends AbstractSequentialList<E>
implements List<E>, Deque<E>, Cloneable, java.io.Serializable
{...}
Sign up to request clarification or add additional context in comments.

Comments

3

Its in parent class of LinkedList: AbstractSequentialList

Comments

2

As far as

LinkedList<E> extends AbstractSequentialList<E>

realization of this method in AbstractSequentialList.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.