Meaningless Drivel is fun!
The moose likes Beginning Java and the fly likes Implementation of Interfaces Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Implementation of Interfaces" Watch "Implementation of Interfaces" New topic

Implementation of Interfaces

liliana punkt

Joined: Dec 23, 2002
Posts: 16
I've got the problem, that I do not know, when I have to implement a method and when it is already implemented (regarding Interfaces):
1. Example
Vector v=new Vector();
Enumeration e=v.elements();
Enumeration is an Interface, but its methods are already implemented. Where? I do not see, that Vector implements Enumeration.
2. Example
all the Listener-Interfaces have to be implemented.
How can I decide, when Implementation is necessary and when it is already done?
thanks for your help,
Francis Siu
Ranch Hand

Joined: Jan 04, 2003
Posts: 867
hi liliana
when I have to implement a method and when it is already implemented
I think there are many methods but the one is sample,you can check API on here to check it whether is already implemented or not.

Francis Siu
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451
Hi liliana,
Welcome to JavaRanch. The only time you need to provide an implementaion for an interface is when you want to create a custom class that will abide by the contract of the interface. In the case of the Java API, the implementations are already provided for you when a method returns an interface as in the case you mentioned Vector.elements(). Here is the 1.4 implementation of that method:

As you can see it just returns an anonymous inner class which implements Enumeration.

Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius - and a lot of courage - to move in the opposite direction. - Ernst F. Schumacher
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
Any time an interface is returned to you, it must be a class that implements the interface that is actually being returned because an interface by itself can not be instantiated.
Iterator myMethod()
The object that I am returning in the above method must be a class that fully implements the Iterator interface. I could not instantiate it and return it to you otherwise.
By the way, here's the code that the Vecotr uses to return an Enumeration. It creates one by using an anonymous class that implements the methods of the Enumeration interface:

Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
liliana punkt

Joined: Dec 23, 2002
Posts: 16
thank you for your help!
The code gave me a good impression of the implementation. It is much clearer now!
I agree. Here's the link:
subject: Implementation of Interfaces
jQuery in Action, 3rd edition