Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Implementation of Interfaces

 
liliana punkt
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
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();
e.hasMoreElements();
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,
liliana
 
Francis Siu
Ranch Hand
Posts: 867
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Michael Morris
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
liliana punkt
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you for your help!
The code gave me a good impression of the implementation. It is much clearer now!
liliana
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic