This week's book giveaway is in the Agile and other Processes forum. We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line! See this thread for details.
My question concerns code like the following fragment:
TreeSet ts = new TreeSet(); // Populate ts for (Iterator it = ts.iterator(); it.hasNext(); ) { System.out.println("TreeSet element: " + it.next()); } // for Iterator it Iterator is an interface and can't be instantiated, so "Iterator it = new Iterator();" doesn't compile. I fail to see the real distinction between this line and the code above, where it does appear that an Iterator object is being instantiated. Why does it make a difference whether you use the new keyword or call a method that returns an Iterator? If Iterator can't be instantiated, how is it that *any* method can return an Iterator? [This message has been edited by Jim Yingst (edited May 08, 2001).]
Ashwin Desai
Ranch Hand
Joined: Jul 17, 2000
Posts: 124
posted
0
Hi, Iterator it = ts.iterator(); The above statement returns you an implementation of an iterator. i.e. a class that implements the Iterator interface. We need not be concerned with the actual class, since we are only concerned with the methods of the Iterator interface that the class has implemented.
Hope this helps. Ashwin
Laojar Chuger
Ranch Hand
Joined: Dec 20, 2000
Posts: 111
posted
0
From the design of Collection, iterator should be created by classes implementing the collection interface.
Dave Brookes
Greenhorn
Joined: Feb 23, 2000
Posts: 17
posted
0
Hi, Iterator it simply declares a typed reference variable which may then be associated with an instance of any class that implements the Iterator interface. Polymorphism in action and very powerful too! Regards, Dave
Regards,<br />Dave
Anonymous
Ranch Hand
Joined: Nov 22, 2008
Posts: 18944
posted
0
In the sentence: Iterator it = ts.iterator(); You declare a reference it to an object that implements that Iterator interface. Since: ts.iterator() does it, there's no problem because there isn't an instance.
Thomas Paul
mister krabs
Ranch Hand
Joined: May 05, 2000
Posts: 13974
posted
0
Look at this code:
So now when I instantiate a RealList object and ask for an Iterator, it actually gives me a RealIterator. List list = new RealList(); Iterator i = list.iterator(); i is actually a pointer to a RealIterator object.
[This message has been edited by Thomas Paul (edited May 09, 2001).]
The point is that the Iterator interface does not know how to make a REAL iterator. It is just a specification for the methods that an iterator must have. However the TreeSet class that implements Iterator DOES know how to make a REAL iterator for itself, and provides all the methods that an iterator must have to work as an iterator. The variable that is holding the iterator has the "type" iterator, which is a nice feature of polymorphism. This iterator type variable can hold real iterators from any class that implements interator, even though none of the objects that it holds is strictly an instance of iterator. In this case it is a TreeSet which "has an" iterator.
"JavaRanch, where the deer and the Certified play" - David O'Meara
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.