This week's giveaway is in the EJB and other Java EE Technologies forum. We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line! See this thread for details.
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
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.
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
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: