Interfaces

An interface is a type with no state. It declares methods that an implementing class supplies, and since Java 8 it may also declare default methods with a body and static methods. A class can implement any number of interfaces, which is how Java gets multiple inheritance of type without the problems of multiple inheritance of state.

An interface

The declaration uses interface instead of class. Its methods are public and abstract unless they are marked default or static, and its fields are constants even without the modifiers. An interface cannot be created with new; it is a contract that a class signs.

The file Greeter.java declares the interface inside the class that uses it, and the interface offers one abstract method, one default method and one static factory:

public class Greeter { interface Greeting { String name(); default String greet() { return "Hello, " + name(); } static Greeting of(String name) { return () -> name; } } public static void main(String[] args) { Greeting person = Greeting.of("Ada"); System.out.println(person.greet()); System.out.println(person.name()); } }

javac -Xlint:all Greeter.java
java Greeter

Hello, Ada
Ada

name() is the abstract part, greet() is the default part that uses it, and of is a static factory that returns an implementation. The lambda is the body of name for an interface with one abstract method.

implements

A class implements an interface with the keyword and must supply every abstract method, or be declared abstract. The implementation method must be public, even though the interface does not write the word: the contract is public, so the implementation is too.

A class may extend a class and implement interfaces at the same time: class Person extends Human implements Named, Aged. The class comes first, and the interfaces follow.

Default methods

A default method gives every implementation a method without forcing each one to write it. The interface can add a method in a later version without breaking the classes that already implement it, which is why the JDK added the feature: the collection interfaces gained methods such as An implementation may override a default method, and it should when the default is not efficient for it. The default is a convenient answer, not a final one.

Static methods and factories

A static method of an interface belongs to the interface and is called on its name, as Greeting.of("Ada") is. It is the place for a factory, a comparator or a helper that belongs with the contract, and it cannot be inherited by an implementing class.

A static factory hides the implementation: the caller asks for a Greeting and does not know which class it gets. The JDK uses the pattern in List.of, Optional.of and Stream.of.

Two interfaces at once

A class may implement several interfaces, and an object of it is an instance of each. The program below has two small interfaces and one class that signs both:

public class TwoInterfaces { interface Named { String name(); } interface Aged { int age(); } static class Person implements Named, Aged { private final String name; private final int age; Person(String name, int age) { this.name = name; this.age = age; } @Override public String name() { return name; } @Override public int age() { return age; } } public static void main(String[] args) { Person person = new Person("Ada", 36); System.out.println(person.name() + " " + person.age()); System.out.println(person instanceof Named); System.out.println(person instanceof Aged); } }

javac -Xlint:all TwoInterfaces.java
java TwoInterfaces

Ada 36
true
true

The two true lines are the type system at work: a Person is a Named and an Aged, so it can be passed to a method that wants either. A class that extends only one class can implement any number of interfaces, which is the reason the interfaces carry most of the types of a Java program.

Exercises

  1. Add a second implementation of greet in one implementation and call both versions.
  2. Add a method

    Questions

    A new set of questions in a new order every time. Choose one answer for each, then check.

    1. What does an interface declare?

      Answer

      A contract of methods that implementing classes supply

      An interface declares method signatures, constants, and optionally default and static methods.

    2. How many interfaces can one class implement?

      Answer

      Any number

      A class has one superclass and can implement any number of interfaces.

    3. What is a default method?

      Answer

      A method of an interface with a body that implementations inherit

      The interface supplies the body so that every implementation gets the method.

    4. What does this program print? public class Q1 { interface Greeting { String name(); default String greet() { return "Hi " + name(); } } public static void main(String[] args) { Greeting g = () -> "Ada"; System.out.println(g.greet()); } }

      Answer

      Hi Ada

      The default method calls the one abstract method, which the lambda implements.

    5. What does this program print? public class Q2 { interface Named { String name(); } static class P implements Named { public String name() { return "Ada"; } } public static void main(String[] args) { Named named = new P(); System.out.println(named.name()); } }

      Answer

      Ada

      The interface reference calls the method the class implements.

    6. What does this program print? public class Q3 { interface A { default String text() { return "a"; } } static class C implements A { @Override public String text() { return "c"; } } public static void main(String[] args) { A a = new C(); System.out.println(a.text()); } }

      Answer

      c

      The class override replaces the default method for that implementation.

    7. What does this program print? public class Q4 { interface A { static String text() { return "static"; } } public static void main(String[] args) { System.out.println(A.text()); } }

      Answer

      static

      A static interface method is called on the interface name.

    8. What does an implementing method have to be?

      Answer

      public

      The interface method is public, so an implementation cannot reduce its visibility.

    9. What is a static factory such as List.of for?

      Answer

      Returning an implementation without naming its class

      The caller asks for the interface type, and the interface chooses the implementation.

    10. What does this program print? public class Q5 { interface A { String text(); } interface B { String text(); } static class C implements A, B { public String text() { return "both"; } } public static void main(String[] args) { A a = new C(); B b = new C(); System.out.println(a.text() + " " + b.text()); } }

      Answer

      both both

      One method satisfies both interfaces, and both references call it.

    The code on this page was built with OpenJDK 21.0.13 (javac and java) and run on Alpine Linux in a Docker container.

    Article author: Arthur Isaev