Java Interface

Summary: in this tutorial, you will learn about the Java interface and how to use it to define a contract for classes.

Introduction to Java interface

In Java, an interface defines a contract for classes. An interface includes a set of abstract methods that can be implemented by any class.

In other words, if a class claims to implement an interface, it must provide implementations for all the abstract methods defined in the interface.

Unlike a class, an interface cannot be instantiated. An interface solely serves as a structure for classes to implement it.

Defining an interface

To define an interface you use the interface keyword, followed by the interface name. Inside an interface, you define one or more abstract methods.

For example, the following defines the Shape interface that has a method called area():

interface Shape {
    double area();
}Code language: Java (java)

Any class that implements the Shape interface must provide an implementation for the area() method.

Implementing an interface

To define a class that implements an interface, you use the implements keywords.

For example, the following shows how to define a Square class that implements the Shape interface:

class Square implements Shape {
    private double length;

    public Square(double length){
        this.length= length;
    }

    @Override
    public double area() {
        return this.length * this.length;
    }
}Code language: Java (java)

In this example, the Square class provides the implementation for the area() method and returns the area of the square.

Similarly, the following defines the Rectangle class that also implements the Shape interface:

class Rectangle implements Shape {
    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;
    }
}Code language: Java (java)

The Rectangle class has two fields, length and width. It implements the area() method by returning the area of the rectangle.

Creating objects

Because the Square class implements the Shape interface, you can create a new instance of the Square class and assign it to a variable of the type Shape like this:

Shape shape = new Square(10);Code language: Java (java)

And then you can call the area() method on the shape reference:

double area = shape.area();
System.out.println("The area of the square is " + area);Code language: Java (java)

Since the Rectangle class also implements the Shape interface, you can create a Rectangle object, assign it to the shape variable, and invoke the area() method:

shape = new Rectangle(10, 20); 
area = shape.area(); 
System.out.println("The area of the Rectangle is " + area);Code language: Java (java)

Here’s the complete program:

public class App {
    public static void main(String[] args) {
        Shape shape = new Square(10);
        double area = shape.area();
        System.out.println("The area of the square is " + area);

        shape = new Rectangle(10, 20);
        area = shape.area();
        System.out.println("The area of the Rectangle is " + area);
    }
}Code language: Java (java)

Output:

The area of the square is 100.0
The area of the Rectangle is 200.0Code language: Java (java)

In this example, the Shape interface defines a contract, specifying that any class implementing it must provide an implementation for the area() method.

By having both Square and Rectangle classes implement the Shape interface, we ensure that they adhere to this contract by providing their own implementations of the area() method.

When we create objects of the Square and Rectangle classes, we can treat them uniformly through the Shape interface.

This is called polymorphism in object-oriented programming.

Polymorphism allows objects of different types (Square and Rectangle) to be treated uniformly through a common interface (Shape).

Summary

  • Java interface defines a contract that other classes must follow.
  • Use the interface keyword to define an interface.
  • An interface may include one or more abstract methods, which are methods without implementations.
  • A class that implements an interface must provide implementations for all methods defined in the interface.