Reputation: 526
my question may be newbish or even repetitive, but still i haven't found the answer yet. iterator() method returns an iterator, which has several methods, say, next(). But where are these methods implemented? Interfaces cannot implement methods, right?
For example:
Set keys = selector.selectedKeys();
Iterator it = keys.iterator();
While (it.hasNext())
Also, it
is not a reference to an object, right? What is it then?
Upvotes: 0
Views: 525
Reputation: 3212
You should approach it this way.
Set keys = selector.selectedKeys();
The above statement should be read like this," I have a reference keys which points some implementation of Set interface. The implementation which it points to can be found in the return type of selectedkeys(). The implementation could be a hashSet, linkedHashSet or any other class which implements Set. Also if you check the set interface it extends collections which inturn extends iterable. So from the interface it becomes obvious that any implementation of Set will surely implement iterator()
Iterator it = keys.iterator();
This line should be read as "call the iterator method of the implementing class to which the reference keys points to" So to get the implementation of Iterator you need to first identify the Implementation which keys points to. The source code has all the answers to these questions. Try and dig deeper into the collections source code and you would find the various Set implementations and you can see how Iterator is implemented.
Upvotes: 0
Reputation: 523
In those implementation of collection class, there is an inner class which implements the Iterator interface. It is the inner class that implements all the methods for Iterator interface. Like ArrayList, inside its implementation, there is
private class Itr implements Iterator<E> {
int cursor; // index of next element to return
int lastRet = -1; // index of last element returned; -1 if no such
int expectedModCount = modCount;
public boolean hasNext() {
return cursor != size;
}
@SuppressWarnings("unchecked")
public E next() {
checkForComodification();
int i = cursor;
if (i >= size)
throw new NoSuchElementException();
Object[] elementData = ArrayList.this.elementData;
if (i >= elementData.length)
throw new ConcurrentModificationException();
cursor = i + 1;
return (E) elementData[lastRet = i];
}
...
}
It is better for you to learn it from the jdk source code. I hope this answer can help you.
Upvotes: 0
Reputation: 6570
Think of an Interface as a "contract".
When you write a program and you say "it is an Iterator", what the compiler (or interpreter, whatever) "understands" is that anything that declares a method expected by that Interface will be accepted. This provides flexibility to your program and this what object oriented languages are all about.
Now, when the program needs to be effectively executed, the interpreter will start to search all the internal dependencies to find out what piece of code will actually execute that "contract". That's resolved dynamically by JAVA.
In this case, JAVA will see that "it" is the iterator of "keys". Then it will also see that "keys" is another interface (Set) that is actually referring to a concrete Set implementation (some piece of code that can be actually executed) which is selector.selectedKeys().
But if "selector" is another interface, then JAVA will continue searching until it finds someone that can actually execute the code, and the thing goes on. :-)
Upvotes: 0
Reputation: 69339
If you use an IDE such as Eclipse, you can query the list of classes that implement Iterator
. Just select the word Iterator
in your code and press Ctrl-t. Other IDEs will have their own way of doing this.
You'll see countless different iterators that are included in Oracle non-public code (and from other libraries you use). It is an instance of one of these classes that are returned when you request an iterator from a set.
To find out which particular iterator is used with a certain object type, browse the source and see for yourself! Or print the name by calling .getClass().getName()
on the iterator instance.
For instance, a HashSet
uses a private iterator class HashMap.KeyIterator
, which extends a parent class HashMap.HashIterator
.
Upvotes: 1
Reputation: 619
In your case the Set keys object is a concrete implementation like
All Known Implementing Classes: AbstractSet, ConcurrentSkipListSet, CopyOnWriteArraySet, EnumSet, HashSet, JobStateReasons, LinkedHashSet, TreeSet
and they implement Iterable.
for example the TreeSet imeplements Iterable
All Implemented Interfaces: Serializable, Cloneable, Iterable, Collection, NavigableSet, Set, SortedSet
Upvotes: 0
Reputation: 5265
in short: Set<E>
extends
a superinterface Iterable<E>
and so passes down the methods from Iterable<E>
The object keys
will thus have access to these methods.
Here's the link to the docs.
Upvotes: -1
Reputation: 8058
Interfaces can't implement methods, that's right.
But it
is a reference to an object, specifically one that implements the Iterator
interface.
If the interface says a method returns an iterator, it is the responsibility of whoever creates an implementation of that interface to also provide an implementation of Iterator that can be returned by those methods. They may be able to reuse an existing iterator over their internal data structures, or they may need to create a new one.
Upvotes: 0
Reputation: 46219
You are correct, interfaces can't implement methods.
The beauty of programming towards interfaces is that you don't need to worry about where they are implemented. All you need to know is that Set#iterator()
returns a class that implements the Iterator
interface. This way, if the returned class containing the implementation should change, this won't matter to your code.
Upvotes: 4
Reputation: 3279
iterator()
will actually return an implementation of Iterator
, i.e. an instance of a class that implements Iterator
. Usually this class is different depending on what collection you call iterator()
.
This is the main advantage of interfaces: They just define some contract that implementations must follow (in this case it defines several methods and how they should behave like), but don't actually provide an implementation of that contract, so that the implementation can be defined at the appropriate location.
Upvotes: 1