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

13 Years
Discussion Span
Last Post by stupidenator

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

public void stateChanged(ChangeEvent e)
   //some code goes here

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

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

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.

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

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

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.