interface

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.
LVL 3
InNoCenT_Ch1ldAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

objectsCommented:
you may have >1 implementations.
0
jimmackCommented:
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
InNoCenT_Ch1ldAuthor Commented:
duplicate question, dont post your comment here.

but here>>http://www.experts-exchange.com/Programming/Programming_Languages/Java/Q_20812411.html
0
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

InNoCenT_Ch1ldAuthor Commented:
too late..nvm.
0
jimmackCommented:
You've got to be quicker than that :-)
0
jimmackCommented:
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
InNoCenT_Ch1ldAuthor Commented:
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
InNoCenT_Ch1ldAuthor Commented:
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
InNoCenT_Ch1ldAuthor Commented:
or can anyone of you give me a sample how a reallife working interface looks like..
0
jimmackCommented:
Have a look at the Thread/Runnable example I mentioned above.
0
objectsCommented:
> how a reallife working interface looks like..

Read your ever friendly Javadoc :)
0
TimYatesCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
TimYatesCommented:
*********************NOTE********************
The above is contrived...but you should be able to see how it works :-)

Hee hee...hope it helped ;-)

Tim
0
objectsCommented:
Lots of interfaces defined there:
LayoutManager, Shape, ListMode, TableMode, TreeModel ... the list goes on.
0
jimmackCommented:
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
TimYatesCommented:
hehehehe :-)
0
sciuriwareCommented:
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
InNoCenT_Ch1ldAuthor Commented:
well, ok ok.. now i think i need to post another question how to allocate my pts to all of you.. hehee ;)

-Child-
0
TimYatesCommented:
;-)
0
InNoCenT_Ch1ldAuthor Commented:
Fair or not, i couldnt tell. but guys, millions tks to you all for your comments! ;)

-Child-
Learn to Fly~
0
jimmackCommented:
Thanx ;-)
0
sciuriwareCommented:
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
CEHJCommented:
>>implements the interface DENTIST

This seems quite appropriate as getting clarity seems to have been like pulling teeth for InNoCenT_Ch1ld ;-)
0
jimmackCommented:
>> 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
InNoCenT_Ch1ldAuthor Commented:
>>clarity seems to have been like pulling teeth for InNoCenT_Ch1ld ;-)

well, that's how we learn right? ;)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.