• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Enumeration...

 
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 399
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Kaspar Dahlqvist
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok! Thank you, guys!
I found it in the Hastable class. I still have some doubts about how it actually works, but at least now I know where to look!
Have great days!
/Kaspar
reply
    Bookmark Topic Watch Topic
  • New Topic