Java Interface

Using the keyword interface, you can totally abstract a class interface from its implementation i.e., using interface, you can specify what a class must do, but now how it does it.

Interfaces are syntactically similar to classes, but they lack instance variables, and, as a general rule, their methods are declared without any body. In practice, this means that you can define interfaces that do not make assumptions about how they are implemented. Once it is defined, any number of classes can implement an interface. Likewise, one class can implement any number of interfaces.

How to implement interface ?

To implement an interface, a class must provide complete set of methods required by the interface. However, each class is free to specify the details of its own implementation. By providing the interface keyword, Java allows you to fully utilize the "one interface, multiple methods" aspect of polymorphism.

Why interfaces are designed ?

Interfaces are designed to support the dynamic method resolution at run time. Normally in order for a method to be called from one class to another, both the classes need to be present at compile time so the Java compiler can check to ensure that the method signatures are compatible. And this requirement by itself makes for a static and non-extensible classing environment. Necessarily in a system like this,
functionality gets pushed up higher and higher in class hierarchy so that the mechanisms will be available to more and more subclass. Interfaces are designed to avoid this problem. They disconnect the definition of a method or set of methods from the inherit hierarchy. Since interfaces are in a different hierarchy from classes, it is possible for the classes which are unrelated in terms of the class hierarchy to implement the same interface. This is where the actual power of interfaces is realized.

Define an Interface

An interface is defined much like a class. Following is the simplified general form of an interface :

access interface name
{
   return-type method-name1(parameter-list);
   return-type method-name2(parameter-list);
   
   type final-varname1 = value;
   type final-varname2 = value;
   //...
   return-type method-nameN(parameter-list);
   type final-varnameN = value;
}

When no access modifier is included, default access results, and the interface is only available to other members of the package in which interface is declared.

When interface is declared as public, the interface can be used by any other code. In this case, the interface must be only public interface declared in the file, and the file must have the same name as interface. name is the name of the interface, and can be any valid identifier. Notice that the methods which are declared have no bodies. Then they must end with a semicolon after the parameter list. They are, essentially, abstract methods. Each class that includes such an interface must implement all of the methods.

As the general form shows, variables can be declared within of interface declarations. They are implicitly final and static, meaning they cannot be changed by the implementing class. And they must also be initialized.

Interface Definition Example

Following is an example of an interface definition. It declares a simple interface having one method called trythis() that takes a single integer parameters.

interface Trythis
{
   void trythis(int par);
}

Implement Interfaces

When an interface has been defined, one or more classes can implement that interface.

To implement an interface, simply include the implements clause in class definition, and then create the methods needed by the interface.

Below is the general form of a class that includes the implements clause :

class classname[extends superclass] [implements interface[,interface...]]
{
   // body of the class
}

If a class implements more than one interface, the interfaces are separated by a comma. If a class implements two interfaces that declare the same method, then the same method will be used by the clients of either the interface.

Note - The methods that implement an interface must be declared as public.

Note - The type signature of the implementing method must watch exactly the type signature specified in the interface definition.

Java Interface Example

Following is a small example class that implements the Trythis interface as shown earlier :

class Customer implements Trythis
{
   /* implements Trythis's interface */
   public void trythis(int par)
   {
      System.out.println("trythis called with " + par);
    }
}

Notice here that the trythis() method is declared using the public access modifier.

It is permissible and common for the classes that implement interfaces to define the additional members of their own. For instance, the following version of the Customer implements trythis() and adds the nonIfaceMeth() method:

class Customer implements Trythis
{
   /* implement Trythis's interface */
   public void trythis(int par)
   {
      System.out.println("trythis called with " + par);
   }
   
   void nonIfaceMeth()
   {
      System.out.println("Classes that implement interfaces may also define other members, too.");
   }
}

Java Online Test


« Previous Tutorial Next Tutorial »