• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Enumeration

 
Jennifer Warren
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please some body tell me where are the methods of enumeration interface are defined/ implemented. As we can do as follows.
Vector v = new Vector();
// code which add obj's to vector v.
Enumeration vEnum = v.element();
// use methods of Enumeration interface to read obj's in the //vector v.
thanks in advance
Jennifer.
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
defined in java.util.Enumeration
you have two methods to iterate over the enumeration
public boolean hasMoreElements()
Tests if this enumeration contains more elements.

public Object nextElement()
Returns the next element of this enumeration if this enumeration object has at least one more element to provide
HIH
------------------
Valentin Crettaz
Sun Certified Programmer for Java 2 Platform
 
Jennifer Warren
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Enumeration is an interface and my understanding is that you can not define the methods in interface they have to implemented in some class. I did try to do that on my own by defining my own interface and adding a method with code it gives a compilation error. I hope you understand what i'm saying.
Thanx.
Jennifer.
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I undertsand what you are saying very well, but let's peek at the elements() method in the Vector class

As you can see, the Vector class provides its own implementation of the Enumeration interface by means of the declaration of an anonymous inner class which implements the methods of the Enumeration interface. So the return type is Enumeration (an interface type) but the actual object being returned is an anonymous implementation of the Enumration interface which is why you can access the methods of Enumeration without even knowing how it is implemented, this is the power of interfaces !
HIH

------------------
Valentin Crettaz
Sun Certified Programmer for Java 2 Platform
 
Jennifer Warren
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
that what i expected it to be but could not find the code for the element method in the vector class. Still you've been a great help and now i can sleep.
Thanx.
Jennifer.
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sweet (Java) Dreams

------------------
Valentin Crettaz
Sun Certified Programmer for Java 2 Platform
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic