Java Interface

An interface is a fully abstract contract. It lists methods that a class promises to provide, without saying how they work. A class then uses the implements keyword to sign that contract and supply the actual code. Interfaces are Java's way to let unrelated classes share a common capability.

#Jobseeker

Find matching jobs & Auto apply with AI

Hyring
Jobseeker using Hyring to search, apply, and prepare with AI

Defining and implementing an interface

You declare an interface with the interface keyword. Traditionally its methods have no body: they are implicitly public and abstract. A class that implements the interface must provide a body for each of those methods, otherwise the class will not compile.

A simple interface

interface Drawable {
  void draw(); // implicitly public and abstract
}

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

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

public class Main {
  public static void main(String[] args) {
    Drawable shape = new Circle();
    shape.draw(); // Drawing a circle
  }
}
Note: Methods in an interface are public by default, so when you implement them you must also mark them public. Leaving out public will cause a compile error because you cannot reduce a method's visibility.

Implementing more than one interface

A Java class can only extend one parent class, but it can implement any number of interfaces. This is how Java offers a safe form of multiple inheritance: you inherit capabilities, not conflicting implementations. Separate the interface names with commas.

Multiple interfaces at once

interface Drawable {
  void draw();
}

interface Resizable {
  void resize(double factor);
}

class Photo implements Drawable, Resizable {
  @Override
  public void draw() {
    System.out.println("Showing the photo");
  }

  @Override
  public void resize(double factor) {
    System.out.println("Scaling by " + factor);
  }
}

public class Main {
  public static void main(String[] args) {
    Photo p = new Photo();
    p.draw();
    p.resize(1.5);
  }
}

What an interface can and cannot hold

  • Abstract methods: the method signatures every implementer must define.
  • Constants: fields are implicitly public, static, and final.
  • Default methods: since Java 8, you can supply a body with the default keyword so implementers inherit it for free.
  • Static methods: utility methods that belong to the interface itself.
  • No instance fields and no constructors: an interface has no state of its own.

A default method

interface Greeter {
  String name();

  // default method: implementers get this for free
  default void greet() {
    System.out.println("Hello, " + name());
  }
}

class User implements Greeter {
  @Override
  public String name() {
    return "Sam";
  }
}

public class Main {
  public static void main(String[] args) {
    new User().greet(); // Hello, Sam
  }
}
KeywordUsed withMeaning
extendsclass to classInherit from one parent class
implementsclass to interfaceProvide bodies for a contract
extendsinterface to interfaceOne interface can extend others
defaultmethod in interfaceInterface supplies a default body
Note: Reach for an interface when you want to describe what an object can do without tying it to a class hierarchy. A Bird and an Airplane are unrelated, yet both can implement a Flyable interface.

Exercise: Java Interface

Before default methods were introduced, what was true of all interface method declarations?