| | |
Interfaces
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
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
-Nick Nisi
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
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:
suppose you implement java.awt.event.MouseListener
and only care for the mouseClicked method then, after implementing the class...
then you would do this
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
Java Syntax (Toggle Plain Text)
public void stateChanged(ChangeEvent e) { //some code goes here }
aka:
Java Syntax (Toggle Plain Text)
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
Java Syntax (Toggle Plain Text)
public void mouseClicked(MouseEvent e) { //your code goes here } //i do not care for these methods so i will not do anything with them //but because there are part of this class I MUST override them public void mouseEntered(MouseEvent e) { } public void mouseExited(MouseEvent e) { } public void mousePressed(MouseEvent e) { } public void mouseReleased(MouseEvent e) { }
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.
if we have an interface like this, and we create a class that implements this, we need to override these methods...
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...
when you create an interface, you say that, this interface has these methods, and every class that implements this interface must use this method.
Java Syntax (Toggle Plain Text)
interface flyable { void fly(); void land(); }
if we have an interface like this, and we create a class that implements this, we need to override these methods...
Java Syntax (Toggle Plain Text)
class bird1 implements flyable { void fly() { //flying instructions here.... } void land() { //landing instructions here.... }
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...
![]() |
Similar Threads
- interfaces in java (Java)
- interfaces and extending (Java)
- message passing interfaces (C)
Other Threads in the Java Forum
- Previous Thread: Help with StringTokenizer..
- Next Thread: parsing error (XML file-java-schema)
| Thread Tools | Search this Thread |
Tag cloud for Java
affinetransform android api apple applet application arc arguments array arrays automation binary bluetooth businessintelligence chat class classes client code component database desktop draw ebook eclipse encode equation error event exception file fractal game givemetehcodez graphics gui helpwithhomework html ide image input integer intersect j2me java javaexcel javaprojects jmf jni jpanel julia linked linux list loop mac main map method methods mobile netbeans newbie number object online open-source oracle parameter print problem program programming project properties recursion reference replaysolutions rotatetext scanner score screen scrollbar server set size sms socket sort sql string superclass swing template test threads time tree windows xstream





