Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 461
  • Last Modified:

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.
0
InNoCenT_Ch1ld
Asked:
InNoCenT_Ch1ld
  • 8
  • 7
  • 4
  • +3
3 Solutions
 
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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
 
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

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

  • 8
  • 7
  • 4
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now