What Is an Interface in Java?

An interface is a named contract: a list of method signatures (and optionally default implementations) that any implementing class must provide. Interfaces are the primary tool for polymorphism and decoupled design in Java.

Basic interface

public interface Shape {
    double area();
    double perimeter();
}

public class Circle implements Shape {
    private final double radius;
    public Circle(double radius) { this.radius = radius; }

    @Override public double area()      { return Math.PI * radius * radius; }
    @Override public double perimeter() { return 2 * Math.PI * radius; }
}

public class Rectangle implements Shape {
    private final double w, h;
    public Rectangle(double w, double h) { this.w = w; this.h = h; }

    @Override public double area()      { return w * h; }
    @Override public double perimeter() { return 2 * (w + h); }
}

Code that works with Shape doesn't care whether it has a circle or a rectangle:

void printShape(Shape s) {
    System.out.printf("Area: %.2f, Perimeter: %.2f%n", s.area(), s.perimeter());
}

printShape(new Circle(5));
printShape(new Rectangle(3, 4));

Multiple interfaces

Java does not allow multiple class inheritance, but a class can implement any number of interfaces:

public class Button implements Clickable, Draggable, Serializable { ... }

Default methods (Java 8+)

Interfaces can provide default implementations that classes may override or inherit as-is:

public interface Greeter {
    String name();

    default String greet() {  // default implementation
        return "Hello, " + name() + "!";
    }
}

public class Person implements Greeter {
    private final String name;
    public Person(String name) { this.name = name; }
    @Override public String name() { return name; }
    // inherits greet() without overriding
}

Static methods in interfaces (Java 8+)

public interface Validator<T> {
    boolean validate(T value);

    static <T> Validator<T> notNull() {
        return value -> value != null;
    }
}

Functional interfaces (Java 8+)

An interface with exactly one abstract method is a functional interface and can be implemented with a lambda:

@FunctionalInterface
public interface Transformer<T> {
    T transform(T input);
}

Transformer<String> upper = s -> s.toUpperCase();
System.out.println(upper.transform("hello")); // HELLO

The JDK ships dozens of built-in functional interfaces: Function<T,R>, Predicate<T>, Consumer<T>, Supplier<T>, Comparator<T>, etc.

Interface vs abstract class

InterfaceAbstract class
Multiple inheritanceYes (implements multiple)No (extends one)
State (instance fields)No (only constants)Yes
ConstructorNoYes
Use whenDefining a capability/contractSharing code among related classes