vendredi 17 avril 2020

Purpose of Iterable interface

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 i can walk and it has defined how it is walking.

Similarly why cant any aggreagtion 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 return the Iterator. It does not take any agrument also. If it takes any argumnets, 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?

Aucun commentaire:

Enregistrer un commentaire