Java: Interface

By Xah Lee. Date: . Last updated: .

What is Interface?

A Java Interface defines a named set of methods, but without any implementation. Then, any Class can declare to implement that interface.

If a class C implements interface X and Y and Z, then it effectively means the C has all methods declared in X, Y, Z.

Java Interface can be inherited. That is, a interface Y can extend another interface X. So that, two interfaces may have a parent-child relationship. The parent interface is called superinterface. The child interface is called subinterface.

When a interface extends another, it basically modifies/adds more methods.

Purpose of Interface

The purpose of interface is that it's easier to make sure many classes have a consistent set of the same methods. For example, data structures such as list, array, queue, stack, all need methods such as {addElement, deleteElement, contains, length, …}. Instead of each class define these methods, you can now define one interface called “List” (or any name you chose) with these methods, and have all the classes “implement” this “List”.

Define a Interface

The syntax is the same as defining a class, except that:

  1. To define a interface, use the keyword interface in place of class.
  2. In the methods, there should be no method body.
// example of defining a interface
public interface X {

    public void f(int x);

    public void g(int x);
}

Implement a Interface

To implement a interface, use the keyword implements after the class name.

// example of implementing a interface
public class Y implements X {

    int n = 0;

    public void f(int x) {
        n = n + x;
    }

    public void g(int x) {
        n = n - x;
    }

    public static void main(String[] args) {
        System.out.println("hello");
    }

}

If your class declares to implement a interface X but doesn't have definitions for all of X's methods, the compiler will complain.

Implement Multiple Interface

A class can implement multiple interfaces at the same time. Just use comma to separate them, like this:

public class ClassName implements IFace1, IFace2 {body}

Empty Interface (Marker Interface)

A interface can have 0 methods. When a interface has no method, it's called marker interface.

When a class “implements” such a interface, the class doesn't have to do anything.

The purpose of marker interface is to serve as a label.

For example, the interface RandomAccess is a marker interface.

For example, the classes that implement RandomAccess includes {ArrayList, Stack, Vector}. It shows up in Java documentation. The only purpose of empty interface and implementing empty interface is to indicate that the class has whatever the name of the interface implies. (but without any check or guarantee. It's simply a label)

Extending Interface

Java Interface can be extended. That is, a interface Y can extend another interface X. So that, two interfaces may have a parent-child relationship. The parent interface is called superinterface. The child interface is called subinterface.

public interface B2 extends B1 {
    // modify or add method spec
}

When a interface extends another, it basically modifies/adds more methods.

Relationship of Interface and Class

A class can “implement” many interfaces. For example, the class ArrayList implements: { Serializable, Cloneable, Iterable, Collection, List, RandomAccess}.

A interface can be “implemented” by many classes. For example, Java: Collection is a interface that represents any list/array/set/sequence kind of data structure, and is implemented by many classes.