Hi all! I have a question about the code snippet below... Enumeration e = h.keys(); while (e.hasMoreElements()) System.out.println(e.nextElement());
This works perfectly! The loop loops through all the keys in my hashtable. My question is: where are the implementations of the methods hasMoreElements() and nextElement()? Enumeration is an interface, so the methods are only declared there, not implemented. I can't find those implementations... /Kaspar
Hi Kaspar, Though Enumeration is an interface , the line
returns an object of a class (that implements Enumeration). This is done on the fly and follows the OOP principle of "programming to the Interface" so that it is easier to change the implementation. Hope this helps. Ashwin.
The keys() method of Hashtable returns as object that implements the Enumeration interface. That's all the user of the Enumeration provided by keys() has to know. In the release of Java in front of me at the moment (1.2.2) that object is an instance of the inner class Enumerator inside the class java.util.Hashtable.