# Java: Interfaces

An interface is a list of methods that must be defined by any class which implements that interface.

Similar to abstract class. An interface is similar to a class without instance and static variables (`static final` constants are allowed), and without method bodies. This is essentially what a completely abstract class does, but abstract classes do allow static method definitions, and interfaces don't.

Contractual obligation. When a class implements an interface, it agrees to define all methods of that interface. A class can implement many different interfaces. If a class doesn't define all the methods of the interfaces it agreed to define (by the implements clause), the compiler will give an error message, which typically says something like "This class must be declared abstract". An abstract class is one that doesn't implement all methods it said it would. This solution to this is almost always to implement the methods of the interface (a misspelled method name is a common cause), not to declare it abstract!

A very common use of interfaces is for listeners. A listener is an object from a class that implements the required methods for that interface. You can create anonymous inner listeners, or implement the required interface in any class.

Interfaces are also used extensively in the data structures (Java Collections) package.

## Classes versus Interfaces

Classes are used to represent something that has attributes (variables, fields) and capabilities/responsibilities (methods, functions). Interfaces are only about capabilities. For example, you are a human because you have the attributes of a human (class). You are a plumber because you have the ability of a plumber (interface). You can also be an electrician (interface). You can implement many interfaces, but be only one class.

This analogy fails in one way however. Capabilities (methods) of a class are unchanging (if a class implements an interface, it is implemented for all instances), whereas the human skills we're talking about are dynamic and can be learned or forgotten. The analogy is flawed, as all analogies are, but it gives some idea of a distinction between classes and interfaces.

## Interfaces replace multiple inheritance

A C++ class can have more than one parent class. This is called multiple inheritance. Managing instance variable definitions in multiple inheritance can be really messy, and leads to more problems than solutions. For this reason Java designers chose to allow only one parent class, but allow multiple interfaces. This provides most of the useful functionality of multiple inheritance, but without the difficulties.

## Implementing an Interface

You may implement as many interfaces in a class as you wish; just separate them with commas. For example,

    // Note:
    //   ActionListener requires defining actionPerformed(...)
    //   MouseMotionListener requires defining mouseMoved(...) and MouseDragged(...).

    public class MyPanel extends JPanel implements ActionListener, MouseMotionListener {
        public void actionPerformed(ActionEvent e) {
            /* Method body */
        }
        public void mouseDragged(MouseEvent me) {
            /* Method body */
        }
        public void mouseMoved(MouseEvent me) {
            /* Method body */
        }
        // Everything else in this class.
    }

It is common for a panel that does graphics and responds to the mouse to implement its own mouse listeners (but not action listeners) as above.

## Declaring an interface

For simple programs you are more likely to use an interface than define it. Here is what the java.awt.event.ActionListener interface definition looks something like the following.

    public interface ActionListener {
          public void actionPerformed(ActionEvent e);
    }