Though I am not very new to Java, the following questions just confused me so much that I cannot overcome this.
I searched a lot on web and found some answers too, but they weren’t completely satisfactory.Could anyone please clear my doubts
My confusions are:
1) Vector v = new Vector();
v.addElement("A");
........
........
Iterator iter = v.iterator();
Q1:Iterator is an interface and we cannot instantiate or create object to an interface.Then what should we call the “iter” here and what is it ?
Q1.1: All the methods of the Iterator(e.g. next()) are called by “iter“. Where are these methods defined?
If these methods are defined in a particular inner class of Vector class(as I found on web), what does
the iterator() method return ?Cann’t we call these methods directly by the object ‘v’?
Q1.2: According to Javadoc: Vector implements {Cloneable, Collection, List, RandomAccess, Serializable}
None of these implement Iterator.So how does Vector implements Iterator?
2) In the declaration:
List list = new ArrayList();
Q1: List is an interface and ArrayList is a class. So new ArrayList() creates an instance of ArrayList.
How can this instance be applied to “list” and AGAIN what is it?
Thanks…..
Re 1)
Iterators are indeed typically implemented as a private inner class of the corresponding collection class. That’s why you can’t see the actual implementation class, only the interface. And that is fine, since its implementation details are not the client’s concern. You can check its real type in e.g. a debugger though, or look it up in the source code ofVector, bundled with your JDK installation.Vector does not implement
Iteratoritself – it only returns an instance of the inner class mentioned above, from itsiteratormethod. This inner class instance has a reference to theVectorinstance that created it, and sees its internals, that is why it can iterate through its elements. This is why the Iterator pattern is useful: it allows you to iterate through a lot of different collections (or in factIterables), without needing to deal with their implementation details.Re 2) your
listis exactly what it looks to be: an instance of theArrayListclass. The latter implements theListinterface, thus everyArrayListis aList.is an example of the idiom “program for interfaces, not for implementations”. Since the declared type of
listabove isList, you can use it like any otherListand your subsequent code isn’t dependent on its actual implementation. So if it later turns out that instead of anArrayList, it would better to use e.g. aLinkedListor aCopyOnWriteArrayList, you only need to change the one line of code above, and the rest of your code will still work perfectly. Whereas if you declarelistasand later realise its type should be changed, you need at least to go through all the code using
listand change its type accordingly wherever it is mentioned. Furthermore, some of the code may even use methods specific toArrayList, which aren’t available or work differently in the other implementation class, making the change even costlier.