Easy to Learn Java: Programming Articles, Examples and Tips

Start with Java in a few days with Java Lessons or Lectures

Home

Code Examples

Java Tools

More Java Tools!

Java Forum

All Java Tips

Books

Submit News
Search the site here...
Search...
 
Search the JavaFAQ.nu
1000 Java Tips ebook

1000 Java Tips - Click here for the high resolution copy!1000 Java Tips - Click here for the high resolution copy!

Java Screensaver, take it here

Free "1000 Java Tips" eBook is here! It is huge collection of big and small Java programming articles and tips. Please take your copy here.

Take your copy of free "Java Technology Screensaver"!.

Java: Interfaces

JavaFAQ Home » Java Notes by Fred Swartz Go to all tips in Java Notes by Fred Swartz


Bookmark and Share

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

------ Continues (click the link below)

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);
}

 Printer Friendly Page  Printer Friendly Page
 Send to a Friend  Send to a Friend

.. Bookmark and Share

Search here again if you need more info!
Custom Search



Home Code Examples Java Forum All Java Tips Books Submit News, Code... Search... Offshore Software Tech Doodling

RSS feed Java FAQ RSS feed Java FAQ News     

    RSS feed Java Forums RSS feed Java Forums

All logos and trademarks in this site are property of their respective owner. The comments are property of their posters, all the rest 1999-2006 by Java FAQs Daily Tips.

Interactive software released under GNU GPL, Code Credits, Privacy Policy