1

I´m studying Java classes and inheritance and I had a doubt with interfaces.

LinkedList class implements Serializable, Cloneable, Iterable, Collection, Deque, List, Queue according to Oracle documentation.

Because Iterable is an interface and not a class like LinkedList, it must have implemented a default iterator, mustn´t it? And if understood that right, where could I see the implementation?

4 Answers 4

3

Because Iterable is an interface and not a class like LinkedList, it must have implemented a default iterator, mustn´t it?

not really, if the super class does override the method then the child class doesn't have to...

look at this:

interface IFoo {
    int getFoo();
}

class A implements IFoo {

    @Override
    public int getFoo() {
        // TODO Auto-generated method stub
        return 0;
    }
}

class B extends A {
    // ...
}
class C extends A implements IFoo {
    // ...
}

note how Class c compiles fine even thoe(redundantly) implements Ifoo but doesnt overide the getFoo method(that is only possible cos C extends A)


the class looks like:

public class LinkedList<E>
    extends AbstractSequentialList<E>
    implements List<E>, Deque<E>, Cloneable, java.io.Serializable
{

so in the class hierarchy it looks like enter image description here

where the AbstractCollection looks like:

 public abstract class AbstractCollection<E> implements Collection<E> {

so there is where they do override the methods from interface Iterable<T>

Sign up to request clarification or add additional context in comments.

1 Comment

Well, it doesn't need to implement the method beeing abstract. Actually, its AbstractSequentialList that really implement the Iterator to redirect to the ListIterator interface (implemented in the AbstractList).
2

The short answer is, in the source code.

The longer answer is LinkedList itself does not need to implement the iterator() method if it inherits from another class which may implement it. If you look closer at the javadoc for LinkedList, you can see that LinkedList does not define the iterator() method, but as you implement Iterable it should be there. Looking at the end you can also see the section of inherited methods. Specifically look at "Methods inherited from class java.util.AbstractSequentialList", where iterator() is listed.

So now we have determined that iterator is in fact in java.util.AbstractSequentialList. So to find the implementation you can look at the source code of AbstractSequentialList which is:

public Iterator<E> iterator() {
    return listIterator();
}

Now as you see the implementation depends on the implementation of listIterator(). LinkedList does not implement the listIterator() method (it has one with one argument but this expects the no-argument method). So looking at the javadoc again we can find under "Methods inherited from class java.util.AbstractList" that the listIterator() method is inherited from there. So looking at the source code from AbstractList:

public ListIterator<E>  listIterator() {
    return listIterator(0);
}

Now, the listIterator(int) method is implemented in the LinkedList class. From the source code of LinkedList:

 public ListIterator<E>  [More ...] listIterator(int index) {
     checkPositionIndex(index);
     return new ListItr(index);
 }

If you need to further analyze what is does, you can continue from there.

1 Comment

I knew that you can implement your own iterator (I have done it for a subject at university, but the class wasn´t LinkedList), but I wasn´t sure at all about a default implementation in LinkedList, now I am
0

You will find the implementation of iterable interface methods in Linked List class itself.

1 Comment

OK but maybe you should include some code to make this a bona fide answer.
0

So LinkedList class extends AbstractSequentialList class. This class has method which is called as iterator. Since LinkedList exteds the AbstractSequentialList class we can make use of iterator method.

public abstract class AbstractSequentialList<E>
extends AbstractList<E>{
  public Iterator<E> iterator(){
    //implementation
 }
}

Now LinkedList class extends the AbstractSequentialList class. so by making the object of LinkedList class we can make use of iterator method.

public class LinkedList<E>
extends AbstractSequentialList<E>
implements List<E>, Deque<E>, Cloneable, Serializable{
}

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.