mukund
mukund

Reputation: 2980

Where the implementation of interface methods in Java's Collection Framework take place?

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.....

Upvotes: 1

Views: 703

Answers (2)

Péter Török
Péter Török

Reputation: 116266

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 of Vector, bundled with your JDK installation.

Vector does not implement Iterator itself - it only returns an instance of the inner class mentioned above, from its iterator method. This inner class instance has a reference to the Vector instance 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 fact Iterables), without needing to deal with their implementation details.

Re 2) your list is exactly what it looks to be: an instance of the ArrayList class. The latter implements the List interface, thus every ArrayList is a List.

List list = new ArrayList();

is an example of the idiom "program for interfaces, not for implementations". Since the declared type of list above is List, you can use it like any other List and your subsequent code isn't dependent on its actual implementation. So if it later turns out that instead of an ArrayList, it would better to use e.g. a LinkedList or a CopyOnWriteArrayList, you only need to change the one line of code above, and the rest of your code will still work perfectly. Whereas if you declare list as

ArrayList list = new ArrayList();

and later realise its type should be changed, you need at least to go through all the code using list and change its type accordingly wherever it is mentioned. Furthermore, some of the code may even use methods specific to ArrayList, which aren't available or work differently in the other implementation class, making the change even costlier.

Upvotes: 4

Peter Lawrey
Peter Lawrey

Reputation: 533510

Q1) You can create an instance which implements an interface. iter is a reference to an object which must implement Iterator

Q1.1) They are implemented as a nested classes. You should be able to see that in the source.

iterator() returns a reference to an Iterator.

Object v doesn't implement Iterator.

Q1.2)

None of these implement Iterator.

which is why you can't call any of the method of an Iterator.

So how does Vector implements Iterator

it doesn't. AbstractList has a nest class which implements Iterator

Q1) again?

list is a reference to an object which must implement List which ArrayList does (or null)

BTW: I wouldn't use Vector unless you have to and I would use generics in examples (they have been around for 8 years now ;)

Upvotes: 0

Related Questions