Overriding the methods in Interface

Posted on 1999-01-20
Last Modified: 2013-11-23
import java.util.*;

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

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
   Here i am not providing any implementation for
hasMoreElement() method.

Then what is meaning of interface
Please justify the above question?
Question by:geostp

Author Comment

ID: 1234417
Edited text of question

Accepted Solution

diakov earned 100 total points
ID: 1234418

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.



Expert Comment

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

Expert Comment

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.

Expert Comment

ID: 1234421

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 :-)


Featured Post

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
ArrayIndexOutOfBoundException 9 82
split string containing \r\n in Java 46 44
servlet example 17 32
How to execute a Python program and gather return output in Java 2 30
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

777 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