• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Is hasmoreElements() abstract method in interface Enumeration?

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
according to the definition of interface, all method in interface is abstract and need to be implemented by a class.
However in java api provide by sun. Enumerationi is an interface. we can just use its method without implementation.

For example,
for (Enumeration e= v. element();e.hasMoreElements() {
 
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, actually we can't
Enumeration isn't sprinkled with magic fairy dust and it's not an entity from a parallel universe where the JLS defines that interfaces can be instantiated and can contain implementations. So what's the deal? Well, for instance java.util.Hashtable has a method called keys(), which returns an Enumeration. How? Well, the Hashtable class defines a private named inner class called Enumerator, which implements the Enumeration interface. An instance of this implementation is returned by the keys() method. No dark magic involved here.
[ March 27, 2008: Message edited by: Jelle Klap ]
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch.

Enumeration is rather old-fashioned programming, but you are right, it is an interface and all its methods are therefore abstract. Most people would use an Iterator nowadays, but the principle is the same.

Your base interface has only abstract methods, but what is returned from the enumeration() method or the iterator() method is not abstract. The code of the collection which uses that method implements all the methods of the interface, puts it into a class (probably anonymous) and returns a reference.

Go into your Java installation folder on your PC, find a file called src.zip and unzip it. Find a class which creates an Iterator, eg AbstractList and read the code of its iterator() method.
 
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's the cool thing about interfaces. What's actually returned is a concrete class that implements the Enumeration interface.

That begs the question "What concrete class is it?". The answer is, you as a user of the class don't have to know or care about those details. Furthermore the actual concrete class could be swapped for a different concrete class in the future that maybe allowed for more efficient iteration. Your code would never have to worry about that stuff. You aren't coupled to the concrete implementation, only the interface it implemnts. That can allow for some really flexible designing and leaves the door open for changes as new algorithms and techniques are discovered.
 
hhd chen
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you very much!
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by hhd chen:
Thank you very much!

You're welcome
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic