Solved

Overriding the methods in Interface

Posted on 1999-01-20
5
369 Views
Last Modified: 2013-11-23
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
Comment
Question by:geostp
5 Comments
 

Author Comment

by:geostp
ID: 1234417
Edited text of question
0
 
LVL 8

Accepted Solution

by:
diakov earned 100 total points
ID: 1234418
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
 

Expert Comment

by:mantish
ID: 1234419
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
 
LVL 16

Expert Comment

by:heyhey_
ID: 1234420
>> 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
 
LVL 8

Expert Comment

by:diakov
ID: 1234421
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

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
HTTPSessionBindingListernter not configured in deployment descriptor 2 65
Oracle SQL syntax check  without executing 6 75
login form jsp example 2 37
swing controls 2 16
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question