By this below definition(pre 1.8) of Iterable
,
package java.lang;
import java.util.Iterator;
public interface Iterable<T> {
Iterator<T> iterator();
}
I would say that, Iterable
is dependent on Iterator
interface as shown below,
package java.util;
public interface Iterator<E> {
boolean hasNext();
E next();
default void remove() {
throw new UnsupportedOperationException("remove");
}
}
Standard recommendation is that, any implementation that implements Iterable
interface becomes iterable.
But syntactically, MyClass
can implement interface Iterator
and enable the implementation MyClass
as iterable without explicitly mentioning class MyClass **implements Iterable**{}
, by adding Iterator<T> iterator();
behavior in Iterator
interface instead of Iterable
interface. This would have simplified presenting only one interface(Iterator
). Iterator
can hold all the responsibilities that Iterable
does.
So, What is the rational behind adding Iterator<T> iterator();
behavior in Iterable
(redundant) interface instead of Iterator
interface?
I am not clear with the purpose of introducing Iterable
interface.
Please help me understand.
For me, this is not a duplicate question because this is an answer which talks about violation of SRP.