Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Spring Framework | Web Services | BioInformatics | Java Server Faces | Jboss 3.0 tutorial | Hibernate 3.0 | XML
 
 
Hot Web Programming Job

 

Tutorial Categories: Ajax | Articles | JSP | Bioinformatics | Database | Free Books | Hibernate | J2EE | J2ME | Java | JavaScript | JDBC | JMS | Linux | MS Technology | PHP | RMI | Web-Services | Servlets | Struts | UML

[an error occurred while processing this directive]

Java: Interfaces

An interface is a list of methods that must be defined by any class which implements that interface. It may also define constants (public static final).

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 specifies that it implements an interface, it must define all methods of that interface. A class can implement many different interfaces. If a class doesn't define all methods of the interfaces it agreed to define (by the implements clause), the compiler gives 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. The solution to this is almost always to implement the missing methods of the interface. A misspelled method name or incorrect parameter list is the usual cause, not that it should have been 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 (eg, the "Deadly Diamond of Death") 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);
}

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

0 comments so far (
post your own) View All Comments Latest 10 Comments:
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification

Tell A Friend
Your Friend Name
Search Tutorials

 

 
 
Browse all Java Tutorials
Java JSP Struts Servlets Hibernate XML
Ajax JDBC EJB MySQL JavaScript JSF
Maven2 Tutorial JEE5 Tutorial Java Threading Tutorial Photoshop Tutorials Linux Technology
Technology Revolutions Eclipse Spring Tutorial Bioinformatics Tutorials Tools SQL
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2007. All rights reserved.