News & UpdatesProgrammingWeb programming StoreMy Projects
Links
Affiliates

Java Tutorial – 18 – Interface

An interface is used to specify methods that subclasses must implement. They are defined with the interface keyword followed by a name and a code block. Their naming convention is the same as for classes, which is to have each word initially capitalized. When an interface is not nested inside another type, its access level can be either package-private or public, just as any other top-level member.

interface MyInterface {}

Interface members

The code block for an interface can first of all contain signatures for instance methods. These methods cannot have any implementations. Instead, their bodies are replaced by semicolons. Interface members must always be public, and since this is the default access level in interfaces this modifier can be left out.

interface MyInterface {
  int myMethod(); // method signature
}

The second member that an interface can contain is constants. Any field created in an interface will implicitly be declared as static final, so these modifiers can also be left out.

interface MyInterface {
  int c = 10; // constant
}

In addition to method signatures and constants, an interface can also contain nested containing types, such as classes or other interfaces.

interface MyInterface 
{
  // Types
  class Class {}
  interface Interface {}
  enum Enum {}
}

Interface example

The example below shows an interface called Comparable, which has a single method named compare.

interface Comparable
{
  int compare(Object o);
}

The class below implements this interface, by using the implements keyword after the class name. By convention, the implements clause is placed after the extends clause, if the class has one. Note that although a class can only inherit from one superclass it may implement any number of interfaces, by specifying them in a comma separated list.

class Circle implements Comparable
{
  public int r;
}

Because Circle implements Comparable it must define the compare method. For this class the method will return the difference between the circle radiuses. The implemented method must be public, in addition to having the same signature as the method defined in the interface.

class Circle implements Comparable
{
  public int r;
 
  public int compare(Object o) { 
    return r - ( (Circle)o ).r; 
  }
}

Functionality interface

Comparable demonstrates the first usage of interfaces, which is to define a specific functionality that classes can share. It makes it possible to use the interface members without having to know the actual type of a class. To illustrate, the example below shows a simple method that takes two Comparable objects and returns the largest one. This method will work for all classes that implement the Comparable interface regardless of their type, since the method only uses the functionality exposed through that interface.

public static Object largest(Comparable a, Comparable b)
{
  return (a.compare(b) > 0) ? a : b;
}

Class interface

A second way to use an interface is to provide an actual interface for a class, through which the class can be used. The example below defines an interface for MyClass called MyInterface. This interface only includes the functionality that programmers using MyClass may need.

interface MyInterface
{
  void exposed();
}
 
class MyClass implements MyInterface
{
  public void exposed() {}
  public void hidden() {}
}

The interface type is then used to hold the implementing class, so that the class is only seen through this interface.

public static void main(String[] args)
{
  MyInterface i = new MyClass();
}

This abstraction provides two benefits. First, it makes it easier for other programmers to use the class since they now only have access to the methods that are relevant. Second, it makes the class more flexible since its implementation can change, without being noticeable by other programmers using the class, as long as the interface is followed.

Interface classes

As mentioned previously, an interface can contain nested types, such as classes. In contrast to methods, these types are implemented inside the interface. This can, for example, be used to provide a class that contains static methods useful for implementing classes. These nested types are only visible to classes implementing the interface, and not to objects of those classes.

interface MyInterface
{
  class HelperClass {
    public static void helperMethod() {}
  }
}
Recommended additional reading:
Sams - Teach Yourself Java in 24 Hours