An interface is a pure contract: it lists methods that implementing classes must provide. It says what a class can do, not how. A class implements an interface.
interface Payable {
double MAX_LIMIT = 100000; // constant (public static final)
void pay(double amount); // abstract by default
default void receipt() { // default method (Java 8+)
System.out.println("Payment received");
}
}
class CardPayment implements Payable {
public void pay(double amount) {
System.out.println("Paid " + amount + " by card");
}
}Interface methods are public unless declared private (allowed from Java 9), so a class must declare the methods it implements as public. A class can implement many interfaces (this is how Java supports multiple inheritance of type). Interfaces can extend other interfaces.
What an interface can contain
| Member | Example | Notes |
|---|---|---|
| Abstract method | void pay(double amount); | public abstract is added automatically |
| Constant | double MAX_LIMIT = 100000; | Automatically public static final |
| Default method (Java 8+) | default void receipt() { ... } | Has a body; implementing classes inherit it and may override it |
| Static method (Java 8+) | static boolean isValid(double a) { ... } | Called on the interface: Payable.isValid(500) |
| Private method (Java 9+) | private void log() { ... } | A helper for the default methods only |
An interface cannot have instance attributes or constructors, so you can never write new Payable().
Implementing an interface
A class that implements an interface makes a promise: "I provide every abstract method in this interface". If it leaves one out, it does not compile (unless the class is abstract). In the example, CardPayment provides pay(), and it inherits receipt() from the default method without writing it.
Interfaces as types
Just like an abstract class, an interface can be used as the type of a variable, an array or a parameter:
class CashPayment implements Payable {
public void pay(double amount) {
System.out.println("Paid " + amount + " in cash");
}
}
Payable[] payments = { new CardPayment(), new CashPayment() };
for (Payable p : payments) {
p.pay(1500); // each class's own pay() runs
p.receipt(); // the default method
}Code that uses Payable works with any class that implements it, including classes that have nothing else in common.
One class, many interfaces
A class can extend one class and implement several interfaces: class Duck extends Animal implements Flyable, Swimmable. One interface can also extend others: interface SmartDevice extends Chargeable, Connectable { }.
Interfaces you will meet in Java
Comparable<T>: providescompareTo(), so objects can be sorted.Runnable: providesrun(), for code that runs in a thread.AutoCloseable: providesclose(), used by try-with-resources (see 3.1.5).List,SetandMap: the collections, which are implemented byArrayList,HashSetandHashMap.
Remember
- An interface is a contract: what a class can do, not how.
- Interface methods are public, so implementing methods must be
public. - A class can implement many interfaces; an interface type can refer to any implementing object.