Interfaces

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Mar 2005
Posts: 192
Reputation: stupidenator is an unknown quantity at this point 
Solved Threads: 4
stupidenator's Avatar
stupidenator stupidenator is offline Offline
Junior Poster

Interfaces

 
0
  #1
Apr 26th, 2005
I just have a quick, dumb question about interfaces... What are they? I mean my teacher has been trying to explain them and I didn't pick up on that and then I read about them in a Java book and on the internet, but it's still just not clicking. I know that you just put the names of the methods in there with a semi-colon at the end because they are abstract, and then you implement them into your program, but I don't understand the point. Can anyone help me out? Thanks ahead of time!

-Nick Nisi
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 348
Reputation: paradox814 is an unknown quantity at this point 
Solved Threads: 4
paradox814's Avatar
paradox814 paradox814 is offline Offline
Posting Whiz

Re: Interfaces

 
0
  #2
Apr 26th, 2005
interfaces are classes which are abstract, which means if you implement a class you MUST override every single method in that class

for instance suppose you have a JSlider and want to add an adction listener that means you need to implement ChangeListener

looking at that class there is only 1 method in there
  1. public void stateChanged(ChangeEvent e)
  2. {
  3. //some code goes here
  4. }
if you want to implement the class but are not ready to override the class, or there are some methods you dont care to override you can just leave them blank
aka:
  1. public void stateChanged(ChangeEvent e) { }


suppose you implement java.awt.event.MouseListener
and only care for the mouseClicked method then, after implementing the class...

then you would do this
  1. public void mouseClicked(MouseEvent e)
  2. {
  3. //your code goes here
  4. }
  5.  
  6. //i do not care for these methods so i will not do anything with them
  7. //but because there are part of this class I MUST override them
  8. public void mouseEntered(MouseEvent e) { }
  9. public void mouseExited(MouseEvent e) { }
  10. public void mousePressed(MouseEvent e) { }
  11. public void mouseReleased(MouseEvent e) { }
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 121
Reputation: tonakai is an unknown quantity at this point 
Solved Threads: 11
tonakai's Avatar
tonakai tonakai is offline Offline
Junior Poster

Re: Interfaces

 
0
  #3
Apr 26th, 2005
here is an another explanation:
when you create an interface, you say that, this interface has these methods, and every class that implements this interface must use this method.

  1. interface flyable {
  2. void fly();
  3. void land();
  4. }

if we have an interface like this, and we create a class that implements this, we need to override these methods...

  1. class bird1 implements flyable {
  2. void fly() {
  3. //flying instructions here....
  4. }
  5. void land() {
  6. //landing instructions here....
  7. }

so if someone wants to use bird1 class, he/she will see that it implements flyable, so he/she will know what methods can be called (fly() and land() in this case)

so interface lets you make "interfaces" between your classes. that will help communication of classes...
Reply With Quote Quick reply to this message  
Join Date: Mar 2005
Posts: 192
Reputation: stupidenator is an unknown quantity at this point 
Solved Threads: 4
stupidenator's Avatar
stupidenator stupidenator is offline Offline
Junior Poster

Re: Interfaces

 
0
  #4
Apr 26th, 2005
Wow, it makes sense now! Thank you so much to both of you!
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Java Forum
Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC