Solved

interface

Posted on 2003-12-01
25
455 Views
Last Modified: 2010-03-31
why does one create an interface? since the method(s) still needed to be implement inside the class that implements the interface?

i feel like it is just another redundant work. please, i need someone to explain to me...

maybe i am damn wrong, but tell me why i am wrong.
0
Comment
Question by:InNoCenT_Ch1ld
  • 8
  • 7
  • 4
  • +3
25 Comments
 
LVL 92

Expert Comment

by:objects
ID: 9848737
you may have >1 implementations.
0
 
LVL 15

Assisted Solution

by:jimmack
jimmack earned 25 total points
ID: 9848747
First, delete your duplicated questions.  No reason to waste points ;-)

Multiple interfaces can be "implement"ed by a class, but a class may only "extend" one other class.  eg.

public class MyClass extends Thread implements MouseListener, ActionListener

I've picked the Thread class for this example because it serves us well :-)  Sometimes, you may want a class to use the facilities that Thread provides, but you *have* to extend another class, eg:

public class MySubClass extends MyBaseClass.  In this case, you can't extend Thread as well :-(

So... You "implement" the Runnable interface:

public class MySubClass extends MyBaseClass implements Runnable
{
   public void run()
   {
   }
}

That's just one example of where interfaces are useful.  For more detail, have a look at:

http://java.sun.com/docs/books/tutorial/java/interpack/interfaces.html

The first link should answer your question in detail ;-)
0
 
LVL 3

Author Comment

by:InNoCenT_Ch1ld
ID: 9848762
duplicate question, dont post your comment here.

but here>>http://www.experts-exchange.com/Programming/Programming_Languages/Java/Q_20812411.html
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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.

 
LVL 3

Author Comment

by:InNoCenT_Ch1ld
ID: 9848767
too late..nvm.
0
 
LVL 15

Expert Comment

by:jimmack
ID: 9848787
You've got to be quicker than that :-)
0
 
LVL 15

Expert Comment

by:jimmack
ID: 9848794
I *think* I'm right in saying that if no one else has commented on your other questions, you should be able to delete them (the delete link should be just above the box where you enter your comments).
0
 
LVL 3

Author Comment

by:InNoCenT_Ch1ld
ID: 9848799
ok, but eventhough we implements the interface, we still need to define its method right?

so why not we just write the method in our class directly?
coz you see

public interface MyInterface{

public void method1(){}
}

public class MyFrame extends JFrame implements MyInterface{

  //any piece of code
 

  //here we define the method
  public void method1(){System.exit(0);}

}

in above case, the MyInterface really does nothing good right? so normally, when you all experts write an interface class, what "else" you put inside it?

sorry for such a silly question but i just trying to understand how is it useful for me...
0
 
LVL 3

Author Comment

by:InNoCenT_Ch1ld
ID: 9848804
haha, sorry for my duplicate questions, i did that unintentionally.. my network down and i click on the submit button a bit too many times.. ;p

-Child-
0
 
LVL 3

Author Comment

by:InNoCenT_Ch1ld
ID: 9848813
or can anyone of you give me a sample how a reallife working interface looks like..
0
 
LVL 15

Expert Comment

by:jimmack
ID: 9848843
Have a look at the Thread/Runnable example I mentioned above.
0
 
LVL 92

Expert Comment

by:objects
ID: 9848856
> how a reallife working interface looks like..

Read your ever friendly Javadoc :)
0
 
LVL 35

Accepted Solution

by:
TimYates earned 25 total points
ID: 9848861
public interface Animal {
  public int countPulse() ;
}

public interface Vegetable {
  public int waterPerHour() ;
}

public class Cat implements Animal {
  public int countPulse() {
    return 100 ;
  }
}

public class Dog implements Animal {
  public int countPulse() {
    return 100 ;
  }
}

public class Tree implements Plant {
  public int waterPerHour() {
    return 50 ;
  }
}

public class Bush implements Plant {
  public int waterPerHour() {
    return 10 ;
  }
}

public class Main {
  Cat cat = new Cat() ;
  Dog dog = new Dog() ;
  Tree tree = new Tree() ;
  Bush bush = new Bush() ;

  public static void doObject( Object o )
  {
     if( o instanceof Animal )
        System.out.println( ((Animal)o).countPulse() ) ;
     else if( o instanceof Plant )
        System.out.println( ((Plant)o).waterPerHour() ) ;
     else
        System.out.println( "Not an Animal or a Plant" ) ;
  }

  public static void main( String[] args )
  {
     doObject( cat ) ;
     doObject( dog ) ;
     doObject( tree ) ;
     doObject( bush ) ;
  }
}
0
 
LVL 35

Expert Comment

by:TimYates
ID: 9848868
*********************NOTE********************
The above is contrived...but you should be able to see how it works :-)

Hee hee...hope it helped ;-)

Tim
0
 
LVL 92

Expert Comment

by:objects
ID: 9848869
Lots of interfaces defined there:
LayoutManager, Shape, ListMode, TableMode, TreeModel ... the list goes on.
0
 
LVL 15

Expert Comment

by:jimmack
ID: 9848905
public class GeorgeBush extends Bush {
  public int waterPerHour() {
    return 5 ;
  }
}

public class EEExpert implements Animal, Plant
  public int countPulse() {
    return Integer.MAX_VALUE ;

  public int waterPerHour() {
    return 0 ;
  }
}

;-)
0
 
LVL 35

Expert Comment

by:TimYates
ID: 9848918
hehehehe :-)
0
 
LVL 24

Assisted Solution

by:sciuriware
sciuriware earned 25 total points
ID: 9849058
I see it like this: an interface is a qualification of a class with restricted meaning.
A reference to an interface is in effect an interface to a class with at least what I need at that moment.

For instance: you can present any of your classes to an eventhandler if you implement the interface
that "defines" the interest of that handler.
A real life example: you got a rotten molar and all you want is someone who "implements the interface DENTIST",
you don't care that he/she is also a certified carpenter (you better not know this).
So any "...Listener" is only interested in some class that implenents the method it wants to call.

If this mechanism did not exist, you would be forced to use fixed classes for any purpose, and
it was hard for you to make them all work together.
;JOOP!
0
 
LVL 3

Author Comment

by:InNoCenT_Ch1ld
ID: 9849079
well, ok ok.. now i think i need to post another question how to allocate my pts to all of you.. hehee ;)

-Child-
0
 
LVL 35

Expert Comment

by:TimYates
ID: 9849091
;-)
0
 
LVL 3

Author Comment

by:InNoCenT_Ch1ld
ID: 9849130
Fair or not, i couldnt tell. but guys, millions tks to you all for your comments! ;)

-Child-
Learn to Fly~
0
 
LVL 15

Expert Comment

by:jimmack
ID: 9849134
Thanx ;-)
0
 
LVL 24

Expert Comment

by:sciuriware
ID: 9849175
To be honest .......................... I present this to my manager and say: "I fulfilled my duty in selfeducation and coaching others, didn't I?"

By the way: the abstract class vs. interface dilemma is very popular in the JAVA world.
;JOOP!
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9849271
>>implements the interface DENTIST

This seems quite appropriate as getting clarity seems to have been like pulling teeth for InNoCenT_Ch1ld ;-)
0
 
LVL 15

Expert Comment

by:jimmack
ID: 9849342
>> seems to have been like pulling teeth

;-)

I'd be interested to see what you guys have to say with regard to InNoCenT_Ch1ld's other question:

http://www.experts-exchange.com/Programming/Programming_Languages/Java/Q_20812487.html

Specifically, TimYates, since you seem to have experience with struts and CEHJ, since you seem to be looking in to it.
0
 
LVL 3

Author Comment

by:InNoCenT_Ch1ld
ID: 9854791
>>clarity seems to have been like pulling teeth for InNoCenT_Ch1ld ;-)

well, that's how we learn right? ;)
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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
Way to decrease size of apk file 9 88
ejb message driven bean mdb creation steps 2 24
Bot application - advice 3 39
Setup GlassFish 4 22
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
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…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

820 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