0

I've read many of the questions on this topic, I still don't understand what these interfaces are and how they work. What I've gathered is that the Iterable interface says that objects instantiated from this class can be iterated over, like in a for-each loop. And this iterable interface has a single abstract method that must be implemented and that is the method is called iterator. But unlike other methods this method is also an interface? and can be instantiated to create an iterator object, I think? But if iterator is an abstract method how are objects created from it? Inside this abstract method contain methods such as hasNext(), remove() and next(). This makes no sense to me how can an abstract method from an iteraface be instantiated and also have three abstract methods of its own?

I am very confused by all this. I've read the answers on here and JavaDocs but this is not making any sense to me.

Please help Thank you

yre
  • 285
  • 4
  • 12
  • "this method is also an interface" - no, it **returns** an object that implements an interface. – Oliver Charlesworth Mar 28 '17 at 14:57
  • @OliverCharlesworth Thanks. This is so confusing for me. So first we implement the iterable interface, which is like saying objects of this class can be the focus of a for-each loop, like arrays are. Once we write "implements iterable<>" when then have to implement a nested interface called interator? Does this use inner classes for this to work? – yre Mar 28 '17 at 15:00
  • 1
    Yup, that's basically correct. Though it isn't "nested", and doesn't have to be implemented using an inner class (although that's a very common way to do it). – Oliver Charlesworth Mar 28 '17 at 15:02
  • @OliverCharlesworth Still not quite understanding. Iterable is an interface with the single abstract method iterator. When we implement the method iterator it has as a return type Iterator, but what exactly is this return type we haven't defined what an Iterator is, so how can we return somethind random like Iterator when implementing the abstract protoype method Iterator of the Iterable iteraface? Thanks! – yre Mar 28 '17 at 15:11
  • @OliverCharlesworth Also a huge question I have here is how is iterator an interface when it's the abstract prototype method of the iterable interface? – yre Mar 28 '17 at 15:14

0 Answers0