wood burning stoves*
The moose likes Beginning Java and the fly likes Interface Doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Interface Doubt" Watch "Interface Doubt" New topic
Author

Interface Doubt

Prakash Pillai
Greenhorn

Joined: Feb 17, 2002
Posts: 12
Hi ,
I need to know more about the interface espically ,Interface like enumration,it has two methods

1.hasMoreElements()
2.nextElement()
Check out the following code
Enumeration en = v.elements();
while(en.hasMoreElements())
{
Employee e = (Employee)en.nextElement();
if(e.name=="Prakash")
e.showDetails();
}
How these methods hasMoreElements()and nextElement work ? as it doesn't have the Body.

Thanking you,
Prakash
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
Originally posted by Prakash Pillai:

How these methods hasMoreElements()and nextElement work ? as it doesn't have the Body.

That's just it! When you implement an interface, you are agreeing to include its structure in your class. It is up to you to write the methods - including their bodies.
Interfaces like Enumeration and Comparable include "expectations" about what and when specific methods return certain values. The Java API Specification on Enumeration describes clearly what is expected from each method. Again, it is up to you to write the methods specific to your class, to act as exptected by the interface.
Am I making sense?
Good Luck,
-Dirk Schreckmann
[ February 19, 2002: Message edited by: Dirk Schreckmann ]

[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Jennifer Wallace
Ranch Hand

Joined: Nov 30, 2001
Posts: 102
But how ? The person is not specifying any particular implementation for the methods and yet the methods function.. they return the next element and test if there is any more element left in the collection. ?
Does that mean there is no code inside the methods yet.. they do what they do?
I am failing to understand this.
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
The word "implements" means "to provide working code for".
So if you say
class A implements B{
You are saying
"class A provides working code for the methods promised in interface B"
All classes that implement enumeration have to actually IMPLEMENT the methods of that interface (make them work). Therefore you know that you can call those methods on those classes and get a working result.
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
Originally posted by Jennifer Wallace:
But how ? The person is not specifying any particular implementation for the methods and yet the methods function.. they return the next element and test if there is any more element left in the collection. ?
Does that mean there is no code inside the methods yet.. they do what they do?
I am failing to understand this.

Hello,
Let's look in more detail at an example.
java.math.BigInteger implements Comparable (an interface). The code for Comparable is simply:

The method compareTo doesn't actually do anything (yet). The documentation for Comparable tells us what is expected from a class that implements Comparable. Here is an excerpt:

compareTo

public int compareTo(Object o)

Parameters:
o - the Object to be compared.

Returns:
a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object.

Now, looking at the code for BigInteger, we can see that the class BigInteger provides the code necessary to do what Comparable requires.

One obvious reason why a class that implements Comparable must specify it's own code for the compareTo method is simply because the particulars of each class are unique to that class. It would be a very tall order for an outside interface to look into BigInteger and decide how one BigInteger object compares to another. The BigInteger class does the work of describing how one instance of BigInteger compares to another. By implementing Comparable, BigInteger just expresses that it agrees to provide the appropriate methods to conform to what Comparable is trying to accomplish.
Ok, I'm getting dizzy.
Good Luck,
-Dirk Schreckmann
P.S. Is someone trying to correct this very annoying bug where, when editing a post, parts of the original post are missing - especially concerning [ quote ] blocks?
[ February 19, 2002: Message edited by: Dirk Schreckmann ]
Jennifer Wallace
Ranch Hand

Joined: Nov 30, 2001
Posts: 102
Thanks for the effort Dirk! I really couldn't relate to the BigInteger thing because I have never used it.
So I was hunting the API to find out which class implements the Iterator interface (more familiar to me). Couldn't find directly where the implementation of Iterator or Enumeration interface was done in the API Docs ..
But Thanks to you, I could open the src.jar and now I found it!

Just sharing it, in case there are others wondering like me!
Take Vector class as Example,
Vector class implements Enumeration interface
as an Anonymous inner class in the method elements()
And iterator interface is implemented in AbstractList as a private inner class.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Interface Doubt
 
Similar Threads
Is it possible to create a class like this
Enumeration...
Did you see this ?StringTokenizer
Enumeration Interface Implementation
Implementatoion of hasMoreElemnts()???