Skip to content
Tutorials Center
My learning

Topics

Java Reference ยท Keywords and Statements

The interface keyword

Reference for the Java interface keyword: abstract, default, static and private methods, constants, functional interfaces, and when to choose an interface.

An interface describes a contract: a set of methods that implementing classes promise to provide. It says what an object can do without saying how. A class can implement many interfaces, which is how Java gets flexible polymorphism without multiple inheritance of state.

Since Java 8, interfaces can also contain default methods (with a body, inherited by implementers) and static methods. Java 9 added private methods for sharing code inside the interface.

Java
interface Greeter {
    String name();                              // abstract
    default String greet() { return "Hi, " + name(); }
}

Try it

Java Implementing and using an interface

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Java Default method conflicts and constants

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Syntax

Java
[public] interface Name [<T>] [extends A, B] [permits X, Y] {
    int CONSTANT = 1;                    // public static final
    ReturnType abstractMethod(params);   // public abstract
    default ReturnType method() { }      // public, inherited
    static ReturnType util() { }         // public, called on the interface
    private ReturnType helper() { }      // Java 9+
}
Member Rules
Fields Always public static final; must be initialized.
Abstract methods Implicitly public abstract. Implementers must declare them public.
default methods Implementers inherit them and may override them.
static methods Called as Name.method(); not inherited.
private methods Helpers for default and static methods.
Nested types Allowed; implicitly public static.

Usage notes

  • No instance state: interfaces cannot have instance fields or constructors.
  • Diamond conflict: if two interfaces provide the same default method, the class must override it and may choose with Interface.super.method(). A class method always wins over an interface default.
  • Functional interfaces have exactly one abstract method and can be targets of lambdas and method references; @FunctionalInterface makes the compiler check it.
  • Marker interfaces such as Serializable have no members and just tag a type.
  • Evolving APIs: default methods let you add a method to a published interface without breaking existing implementers. Think twice before using them for more than that.
  • sealed interfaces restrict who may implement them and work well with pattern matching in switch.
  • Type of reference: program to the interface (List<String> list = new ArrayList<>()), not the implementation.
  • Interface vs abstract class: see abstract. Records and enums can implement interfaces but cannot extend classes.