• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Is hasmoreElements() abstract method in interface Enumeration?

 
hhd chen
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • 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() {
 
Jelle Klap
Bartender
Posts: 1951
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • 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 ]
 
Campbell Ritchie
Sheriff
Posts: 48374
56
  • Mark post as helpful
  • send pies
  • 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.
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Thank you very much!
 
Campbell Ritchie
Sheriff
Posts: 48374
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by hhd chen:
Thank you very much!
You're welcome
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic