Java Interfaces

 

An interface is a kind of data type.  An interface is very much like an abstract class, but with no constructors, method bodies or instance variables. There is never any implementation of any method in an interface!  Most interfaces are composed of a set of methods. Some interfaces contain just data.  As with an abstract class, instances of an interface cannot be created.  Like a class, an interface is an element of a package.  For instance, java.awt contains an interface LayoutManager, defined as follows:

 

public interface LayoutManager

{

     Dimension minimumLayoutSize (Container parent);

     Dimension preferredLayoutSize (Container parent);

     void addLayoutComponent (String name, Component comp);

     void removeLayoutComponent (Component comp);

     void layoutContainer (Container parent);

}

 

 

This looks very much like an abstract class definition, with the keyword interface replacing the keywords abstract class.  All methods and constants in an interface are implicitly public, and the methods are implicitly abstract.

 

One interface can extend another interface in much the same way that one class extends another class.  Notice that a class implements an interface, but an interface extends an interface.

 

Interfaces allow a well-defined communication between objects.  An interface specifies the behaviour of an object as a set of methods.   Implementing an interface is very similar to inheritance.  If a class implements an interface, an object of that class is also a member of the interface data type.  Interfaces are an important part of GUI event handling.  An interface definition begins with the keyword interface and contains a set of public abstract methods.  Interfaces may also contain public static final data.  To use an interface, a class must specify that it implements the interface and the class must define every method in the interface with the number of arguments and the return type specified in the interface definition. If the class leaves one method in the interface undefined, the class becomes an abstract class and must be declared abstract in the first line of its class definition.  If you implement an interface, you have to define all the methods specified by the interface.

 

Note, an interface is not a class and cannot have constructors.

 

An interface is typically used in place of an abstract class when there is no default implementation to inherit – i.e., no instance variables and no default method implementations.  Like public abstract classes, interfaces are typically public data types, so they are normally defined in files by themselves with the same name as the interface and the .java extension.

 

Java does not support multiple inheritance of classes.  This means that there can only be one class name after the keyword extends.  Java does however support the implementation of multiple interfaces (which really is a form of multiple inheritance). This means you can list one or more interface names after the implements keyword.  This goes a long way to provide the advantages of multiple inheritance without the complexities of the coding of multiple inheritance. 

 

Another use of interfaces is to define a set of constants that can be used in many class definitions.  Consider interface Constants

 

public interface Constants

{

     public static final int ONE = 1;

     public static final int TWO = 2;

     public static final int THREE = 3;

}

 

Classes that implement interface Constants can use constants ONE, TWO and THREE anywhere in the class definition. A class can even use these constants by simply importing the interface, then referring to each constant as Constants.ONE, Constants.TWO and Constants.THREE.  Because there are no methods declared in this interface, a class that implements the interface is not required to provide any implementation.

 

An interface, like a class, defines a reference type.  Even though we cannot create instances of an interface or a class (by using the new keyword), we can create variables of type reference-to-interface.  These variables then hold the address of the interface or class.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

This page is:  Java_Interfaces.htm, edited using: Word 2000

Last updated: Thursday 2nd December 2004, 2:21 PT by AHD