public interface Walkable {
void walk();
}
class Animal implements Walkable{
public void walk() {
//defines some way to walk
}
}
In the above example, the Animal class says that it can walk and it has defined how it is walking.
Similarly why cant any aggregation of objects just implement Iterator
interface and imply that it is Iterable
.
class Employee{
Iterator getEmployeeIterator(){
return new EmployeeCollection();
}
}
class EmployeeCollection implements Iterator{
Employee []emp;
public boolean hasNext() {
//implementation
}
public Object next() {
//implementation
}
What is the need of the Iterable
interface which simply returns the Iterator
? It does not take any argument also. If it takes any arguments, I can assume it can work like a factory providing different types of Iterators
Is "providing a common interface" is the sole purpose of Iterable
?