AI Hints
Home›Java Tutorial›Interfaces, Records and Enums
Java · Lesson 79 of 120

Interfaces

A promise of methods.

⏱ 2 min read▶ Video at 1:39:29🎓 Free
Watch this lesson in the Java courseWatch on YouTube ↗Full Java course

An interface is a promise. It lists methods a class promises to have, without saying how they work. Very different classes can keep the same promise.

  • A promise
  • Lists methods
  • Any class can keep it

Payable.java

Payable promises one method, amount due. Like an abstract method, it has no body. A default method does have a body, and every class that keeps the promise gets it for free.

Main.javajava
public interface Payable {
    double amountDue();

    default String summary() {
        return String.format("Due: %.2f", amountDue());
    }
}

Keeping the promise

Implements Payable means this class keeps the promise. So it must write amount due. Employee implements Payable too, with hours times rate. An invoice and an employee have nothing else in common.

Main.javajava
public class Invoice implements Payable {
    private double total;
    public Invoice(double total) { this.total = total; }
    public double amountDue() { return total; }
}

Treating them the same

Because both are Payable, they can sit in one array. The loop doesn't care which is which. Run it. Each bill shows its amount due, and the total is nine hundred and thirty eight pounds, ninety nine.

Main.javajava
public class Main {
    public static void main(String[] args) {
        Payable[] bills = { new Invoice(250), new Employee(38, 15.5), new Invoice(99.99) };
        double sum = 0;
        for (Payable p : bills) {
            System.out.println(p.summary());
            sum += p.amountDue();
        }
        System.out.printf("Total to pay: %.2f%n", sum);
    }
}
Output
Due: 250.00
Due: 589.00
Due: 99.99
Total to pay: 938.99
Also uses Payable.java
public interface Payable {
    double amountDue();

    default String summary() {
        return String.format("Due: %.2f", amountDue());
    }
}
Also uses Invoice.java
public class Invoice implements Payable {
    private double total;
    public Invoice(double total) { this.total = total; }
    public double amountDue() { return total; }
}
Also uses Employee.java
public class Employee implements Payable {
    private double hours, rate;
    public Employee(double hours, double rate) { this.hours = hours; this.rate = rate; }
    public double amountDue() { return hours * rate; }
}

Interface or abstract class

A class can extend only one parent class, but it can implement many interfaces. Use an abstract class when classes share code and fields. Use an interface when unrelated classes share an ability.

Abstract class

  • extends: only oneShares fields and codeAn is-a family

Interface

  • implements: manyA list of abilitiesUnrelated classes

Summary

An interface lists methods a class promises to have. A class can implement many interfaces. Next, records, the shortest way to write a data class.

Key takeaways
  • interface
  • implements
  • Many per class
Next up: Records — tiny data classes