Solved

Overriding the methods in Interface

Posted on 1999-01-20
5
370 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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: 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.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Maven Project: Hibernate Dependencies Conflict 10 53
JAVA API design with micro service cloud in mind 1 82
Java syntax, or is it Selenium 6 43
What browser will run Java? 7 128
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
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 …
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

726 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