wood burning stoves 2.0*
The moose likes Java in General and the fly likes Iterator Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Iterator" Watch "Iterator" New topic
Author

Iterator

Shahir Deo
Ranch Hand

Joined: Mar 19, 2013
Posts: 78

Hi Guys , need some explanation on following code.


Iterator is interface then why we are creating an object "itr" in line no.14 ??


Knowledge enlivens the soul.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

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
Ranch Hand

Joined: Mar 19, 2013
Posts: 78

So , iterator() method belongs to ArrayList Class which implementing Iterator Interface and object 'itr' is an instance of ArrayList?
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

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.
Shahir Deo
Ranch Hand

Joined: Mar 19, 2013
Posts: 78

Thanks Jeff again.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

You're welcome. I hope it's clear now.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Iterator
 
Similar Threads
iterate
User-defined objects in collections
Collection Error
ArrayList