• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 396
  • Last Modified:

Overriding the methods in Interface

import java.util.*;

class myVector
{
  public static void main(String arg[])
  {
      Vetor v = new Vector();
      Enumeration enum = v.elements();
      while(enum.hasMoreElements())
       {
        ---
        ---
       }
    }
}

In the above code 1)i am not implementing the interface like MouseListenere ( I am not giving class myVetor implements Enumeration)
2) I am not overriding the methods of Enumerations interface
   I am straightway using the methods in the while loop
   while(enum.hasMoreElements()).
   Here i am not providing any implementation for
hasMoreElement() method.

Then what is meaning of interface
Please justify the above question?
0
geostp
Asked:
geostp
1 Solution
 
geostpAuthor Commented:
Edited text of question
0
 
diakovCommented:
Hi,

An interface is a special construct in Java. It has almost the same value as a fully abstract class definition. You know, when a class method is defined abstract, you do not define it's body, however, you are obliged to do it if you are extending this class. You cannot explicitly instantiate abstract classes nor interfaces.

However, you can downcast an instance of an existing class to a parent class or an interface being implemented by this class' instance. This way you can call the methods of the class that implements the interface, merely by having an 'instance' (from the downcast) of the interface.

Now, let see when such things are usefull.

For example, let's look at the listeners scheme. You have an interface that defines some event notification methods, as well as some constants (static final variables) that represent the events. Then lets assume you have a class MyWindow deriving from the Window awt class. You want to process window events in MyWindow or in another class, created within MyWindow class. You can do that by making the class that you want to process events
implemet the WindowListener interface. In particular this can be the MyWindow itself. Then, in MyWindow definition you define the event handler methods as you want them to process the events. then you call the parents (Window) addWindowListener(this) and voila, your class MyWindow already receives notifications. The idea is, that the base class Window has the method addWindowListener(WindowListener l) and the 'this' reference you pass is downcasted to the interface value.

In general, you can concider the interfaces as fully abstract class definitions (all methods are abstract). The only visible difference is that in Java you can implement multiple interfaces but extend only one class.

Hope this helped. If yes, I will answer to your other two questions, since they concern the same topic.

If not, tell me what part of the explanation I have to expand or clarify.

Greetings,
  Nik




0
 
mantishCommented:
Hi,
    Lets go back to your snippet of code. As u say ur class doesnt explicitly implement
any  interface & yet the vector class uses the methods in the interface enumeration.
To begin with the the method "elements" of class vector returns an instance of enumeration. which comes to u as no surprise.
But enumeration itself has 2 methods

a) boolean hasMoreElements()
b) Object nextElement();

OK heres the crucial point. The interface Enumeration basically provides  a stub implementation of both the methods.So when  u are call the hasMoreElements( ) method u are basically  using the default implementation of the method.Otherwise like u said
u would have to provide your own implementation for both the methods every time u use
the enum interface .And  if it had'nt been an interface & instead a  class it would have had
to be extended(inherited) & since Java doesnt allow multiple inheritances.MyVector cannot inherit from  any other class.
U might want to check out the implemenation of one the adaptor class which actually
extends the coressponding interface ( like MouseAdaptor extends MouseListener) to
provide stub implementation to the methods in the interface.That way u wouldnt have to
do the stub implementation of the methods urself.  
   
0
 
heyhey_Commented:
>> The interface Enumeration basically
>> provides  a stub implementation of both the methods

interface  does NOT provide any implementation.
all methods defined in interface are abstract i.e. not implemented.

you can't create objects that belong to some Interface - you just create objects from some Class that implements some interface.
0
 
diakovCommented:
mantish,

heyhey_ is right.

Try to stick to the terms JDK states. When you speak of interfaces, never use 'extends' with classes but 'implements' because interfaces are implemented by classes. Interfaces however are extended when another interfaces is defined as a derivative of the one.

Just a silly comment: you are experienced with DCOM/ActiveX I think, since they use stubs on the server side of their IDL interfaces :-)

Cheers,
  Nik
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now