Java Extends Interface

Summary: in this tutorial, you will learn how to define an interface that extends one or more interfaces.

Like a class, an interface can extend another interface. A class can extend only one class, but an interface can extend multiple interfaces.

When you extend an interface, you’re creating a new interface that inherits all the members from the original interfaces. In addition, you can add new methods specific to the extended interface.

This is useful when want to introduce new behavior to classes that implement the extended interface.

If you modify the existing interface by adding new methods or changing the behavior of existing methods, it will break the existing classes that implement the interface.

Therefore, instead of changing the existing interface, you can extend it. This helps you maintain backward compatibility with the classes that already implement the original interface.

Extending an interface

To define an interface that extends another interface, you use the extends keyword:

interface NewInterface extends OriginalInterface {
    //...
}Code language: Java (java)

The following example shows how to define an interface that extends another interface.

First, define an interface Undoable that includes one method undo():

interface Undoable {
    void undo();
}Code language: Java (java)

Second, define another interface Redoable that extends the Undoable interface:

interface Redoable extends Undoable {
    void redo();
}Code language: Java (java)

The Redoable interface has two methods:

  • redo() – This method is defined directly in the Redoable interface.
  • undo() – This method is inherited from the Undoable interface.

If you define a class that implements the Redoable interface, you need to provide implementations for the two methods redo() and undo().

Third, define a class TextEditor that implements the Redoable interface:

public class TextEditor implements Redoable {

    @Override
    public void redo() {
        System.out.println("Redo");
    }

    @Override
    public void undo() {
        System.out.println("Undo");

    }
}Code language: Java (java)

Finally, create a new instance of the TextEditor, assign it to a reference of the Redoable interface, and call the undo() and redo() methods via the reference:

public class App {
    public static void main(String[] args) {
        Redoable r = new TextEditor();
        r.undo();
        r.redo();
    }
}Code language: Java (java)

Output:

Undo
RedoCode language: Java (java)

Extending multiple interfaces

To extend multiple interfaces, you place a list of separated interfaces after the extends keyword like this:

interface NewInteface extends Interface1, Interface2, Interface3{
   // ...
}Code language: Java (java)

When you define a class that implements the NewInteface, you need to provide implementations of all the methods in the NewInteface as well as the ones in the Interface1, Interface2, and Interface3.

Let’s take a look at an example of extending multiple interfaces.

First, define three interfaces Shape, Drawable, and Resizable:

interface Shape {
    double area();
}

interface Drawable {
    void draw();
}

interface Resizable {
    void resize(double scale);
}Code language: Java (java)

Second, define an interface Shape2D that extends the three interfaces:

interface Shape2D extends Shape, Drawable, Resizable{
    void rotate(double degrees);
}Code language: Java (java)

In addition, the Shape2D defines its own method called rotate().

Third, define a Rectangle class that implements the Shape2D interface:

public class Rectangle implements Shape2D {
    private double length;
    private double width;

    public Rectangle(double length, double width) {
        this.length = length;
        this.width = width;
    }

    @Override
    public double area() {
        return this.length * this.width;
    }

    @Override
    public void draw() {
        System.out.println("Drawing the rectangle");
    }

    @Override
    public void resize(double scale) {
        System.out.println("Scaling the rectangle");
    }

    @Override
    public void rotate(double degrees) {
        System.out.println("Rotating the rectangle");
    }
}Code language: Java (java)

In this example, the Rectangle class needs to implement all the methods of the Shape2D interfaces, including the ones that the Shape2D inherited from other interfaces.

Summary

  • Use extends keyword to define an interface that extends another interface.
  • An interface can extend more than one interface.
  • Extending a new interface allows you to add new methods to the new interface and maintain backward compatibility to the existing classes that already implement the original interface.
Was this tutorial helpful ?