Page 1 of 1

iterator() method API

Posted: Wed Jan 03, 2018 3:50 pm
by hsiaok
iterator() is listed as both instance method and abstract method of interface Iterable, like this:

Iterator<T> iterator()
Returns an iterator over elements of type T.
Returns:an Iterator


Under Abstract Method and Instance Method.

I thought an interface has 3 type of methods: default method, static method and abstract method; so my question is why is iterator() is a instance method under Iterable API ?

Re: iterator() method API

Posted: Wed Jan 03, 2018 10:20 pm
by admin
Not really sure what you mean. iterator() is indeed an abstract method in Iterable interface. See this: https://docs.oracle.com/javase/8/docs/a ... iterator--

If any non-abstract class (for example, ArrayList) says it implements Iterable, then it must provide an implementation of this method. In that case, it will be an instance method of that class.