Back up and think for a moment. If we could never do Iterator iter = something;, then what use would interfaces be? How could we ever use them?
Obviously, we have to be able to create something that IS-AN iterator, otherwise the type is pointless and can never be used. That doesn't mean though that we're doing Iterator iter = new Iterator(); That's just as illegal as you would expect.
But consider this. We can do Object x = new Date(); and the reason we can do that is because a Date IS-AN Object. Same with Number n = Integer.valueOf(123); The IS-A relationship applies to classes that implement interfaces just as it applies to classes that extend other classes. Both are just smiple inheritance.
So when we do Iterator iter = list.iterator(); that iterator() method is creating an instance of some class that implements Iterator, and returning a reference to that instance of that concrete class.
Shahir Deo wrote:So , iterator() method belongs to ArrayList Class
It's defined in Iterable, which Collection extends, and which therefore ArrayList implements.
which implementing Iterator Interface and object 'itr' is an instance of ArrayList?
No. There's an object that impelments Iterator, and there's a completely separate object of a completely different class that is the ArrayList. An ArrayList is not an Iterator and an Iterator is not an ArrayList.
The ArrayList class has an iterator() method which returns a reference to an object that implements Iterator. There's a class defined in the Collections Framework--probably a private nested class inside ArrayList--that implements Iterator for ArrayList. When we call ArrayList.iterator(), the ArrayList creates and returns one of these Iterator objects.