Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Interface Doubt

 
Prakash Pillai
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Jennifer Wallace
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic