• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Iterator

 
Shahir Deo
Ranch Hand
Posts: 80
Hibernate Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Guys , need some explanation on following code.


Iterator is interface then why we are creating an object "itr" in line no.14 ??
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 80
Hibernate Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So , iterator() method belongs to ArrayList Class which implementing Iterator Interface and object 'itr' is an instance of ArrayList?
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 80
Hibernate Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Jeff again.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome. I hope it's clear now.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic