Solved

Overriding the methods in Interface

Posted on 1999-01-20
5
364 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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Java 1603 Error 2 42
countPairs challenge 7 58
PDFBox - convert image to PDF, PDF resolution 3 69
maven example error 3 36
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

757 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now