Java Interfaces

A Java interface is a type that defines behavior a class promises to provide. It is like a contract: the interface lists methods, and implementing classes provide the actual code.

Interfaces are useful when different classes should share the same actions, even when they do not belong in the same inheritance family.

Basic Interface Syntax

Use the interface keyword to create an interface. A class uses implements to promise that it will provide the interface methods.

Interface methods are usually written without a method body. These methods are implicitly public and abstract, so the implementing class must make its overriding methods public.

interface Drawable {
    void draw();
}

class Circle implements Drawable {
    @Override
    public void draw() {
        System.out.println("Drawing a circle.");
    }
}

class Button implements Drawable {
    @Override
    public void draw() {
        System.out.println("Drawing a button.");
    }
}

public class Main {
    public static void main(String[] args) {
        Drawable first = new Circle();
        Drawable second = new Button();

        first.draw();
        second.draw();
    }
}

Output:

Drawing a circle.
Drawing a button.

The variables first and second both have the interface type Drawable. Java still runs the correct draw() method for the actual object, which is polymorphism.

Interfaces As Contracts

An interface does not say how the work is done. It only says what methods must exist. In the example above, both Circle and Button can be drawn, but each class draws itself in a different way.

This lets other code depend on the shared ability instead of the exact class. If a method accepts a Drawable, it can work with any current or future class that implements Drawable.

Multiple Interfaces

Java classes can extend only one class, but they can implement multiple interfaces. This is one of the main reasons interfaces are common in Java programs.

Separate interface names with commas after implements. The class must provide all required methods from all listed interfaces.

interface Printable {
    void print();

    default void printTwice() {
        print();
        print();
    }
}

interface Scannable {
    void scan();
}

class OfficePrinter implements Printable, Scannable {
    @Override
    public void print() {
        System.out.println("Printing document.");
    }

    @Override
    public void scan() {
        System.out.println("Scanning document.");
    }
}

public class Main {
    public static void main(String[] args) {
        OfficePrinter machine = new OfficePrinter();

        machine.printTwice();
        machine.scan();
    }
}

Output:

Printing document.
Printing document.
Scanning document.

OfficePrinter implements both Printable and Scannable. It must write the print() and scan() methods.

Default Methods

An interface can include a default method. A default method has a method body, so implementing classes get it automatically unless they override it.

In the second example, printTwice() is a default method. It reuses the required print() method, so any class that implements Printable can call printTwice().

Interface Fields

Fields declared in an interface are automatically public, static, and final. That means they are constants, not normal object fields.

For object data that changes from object to object, use fields in the class that implements the interface.

Interface Vs Abstract Class

Use an interface when you want to describe a shared ability, especially across unrelated classes. Use an abstract class when related classes need a common parent with shared fields, constructors, or reusable code.

  • interface Drawable means “anything that can draw itself.”
  • abstract class Shape means “a kind of shape with shared shape behavior.”
  • A class can implement multiple interfaces, but it can extend only one class.

Common Mistakes

  • Forgetting the implements keyword and writing extends for a class that uses an interface.
  • Leaving off public when overriding an interface method.
  • Trying to create an interface directly with new Drawable().
  • Using interface fields as if they were changeable object variables.

Takeaway: interfaces define shared behavior, and any class that implements an interface promises to provide that behavior.