aspose file tools*
The moose likes Beginning Java and the fly likes Is hasmoreElements() abstract method in interface Enumeration? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Is hasmoreElements() abstract method in interface Enumeration?" Watch "Is hasmoreElements() abstract method in interface Enumeration?" New topic
Author

Is hasmoreElements() abstract method in interface Enumeration?

hhd chen
Greenhorn

Joined: Mar 27, 2008
Posts: 4
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

Joined: Mar 10, 2008
Posts: 1823
    
    7

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 ]

Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39834
    
  28
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

Joined: Jan 17, 2006
Posts: 1296
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.


Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
hhd chen
Greenhorn

Joined: Mar 27, 2008
Posts: 4
Thank you very much!
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39834
    
  28
Originally posted by hhd chen:
Thank you very much!
You're welcome
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Is hasmoreElements() abstract method in interface Enumeration?