Solved

interface

Posted on 2003-12-01
25
452 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
Comment Utility
you may have >1 implementations.
0
 
LVL 15

Assisted Solution

by:jimmack
jimmack earned 25 total points
Comment Utility
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
Comment Utility
duplicate question, dont post your comment here.

but here>>http://www.experts-exchange.com/Programming/Programming_Languages/Java/Q_20812411.html
0
 
LVL 3

Author Comment

by:InNoCenT_Ch1ld
Comment Utility
too late..nvm.
0
 
LVL 15

Expert Comment

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

Expert Comment

by:jimmack
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
or can anyone of you give me a sample how a reallife working interface looks like..
0
 
LVL 15

Expert Comment

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

Expert Comment

by:objects
Comment Utility
> how a reallife working interface looks like..

Read your ever friendly Javadoc :)
0
 
LVL 35

Accepted Solution

by:
TimYates earned 25 total points
Comment Utility
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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 35

Expert Comment

by:TimYates
Comment Utility
*********************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
Comment Utility
Lots of interfaces defined there:
LayoutManager, Shape, ListMode, TableMode, TreeModel ... the list goes on.
0
 
LVL 15

Expert Comment

by:jimmack
Comment Utility
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
Comment Utility
hehehehe :-)
0
 
LVL 24

Assisted Solution

by:sciuriware
sciuriware earned 25 total points
Comment Utility
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
Comment Utility
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
Comment Utility
;-)
0
 
LVL 3

Author Comment

by:InNoCenT_Ch1ld
Comment Utility
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
Comment Utility
Thanx ;-)
0
 
LVL 24

Expert Comment

by:sciuriware
Comment Utility
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
Comment Utility
>>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
Comment Utility
>> 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
Comment Utility
>>clarity seems to have been like pulling teeth for InNoCenT_Ch1ld ;-)

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

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This video teaches viewers about errors in exception handling.

771 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

10 Experts available now in Live!

Get 1:1 Help Now