Interfaces
An interface is a type that names a set of methods a class must provide. It describes what an object can do, not how the object is built.
Detailed Explanation
You want to sort things, send messages, or close resources without caring which class you were handed. An interface is that agreement. Comparable says "you can compare yourself". AutoCloseable says "you can be closed". The class that implements the interface supplies the bodies.
A class may implement many interfaces and extend only one class. That is why interfaces are the usual way to share a role across unrelated types. String, Integer, and your own Rank can all be Comparable without sharing a parent.
Java interfaces grew over time. Originally they held only abstract instance methods and constants (public static final fields). Java 8 added default methods (instance methods with a body) and static methods. Java 9 added private methods to share code between defaults. Java 16 allowed sealed interfaces that list permitted implementations. Java 21's pattern matching uses interfaces as the types you switch on.
Implementing an interface is a promise the compiler checks. Miss a method and the class must be abstract, or compilation fails. Default methods are inherited unless the class overrides them. If two interfaces supply the same default signature, the class must override and pick, usually by calling InterfaceName.super.method().
You reach for an interface when callers should depend on a role, when a second implementation is likely, or when you need a callback (Runnable, Comparator). You avoid a new interface when a single concrete class is the product and no test double is needed. Functional interfaces, those with one abstract method, are also the target type for lambdas.
The cost of a wide interface is every implementor. Adding an abstract method is a breaking change unless you add it as default. That is why the JDK prefers small interfaces (Appendable, Readable) and why default methods exist. Treat the method list as published API. Keep it short. Name methods after outcomes, not after a particular class's internals.
Code Examples
interface Priced {
int paise();
default String label() { // Java 8+: shared behaviour, still overridable
return paise() + " paise";
}
}
class Book implements Priced {
private final int paise;
Book(int paise) {
this.paise = paise;
}
@Override
public int paise() {
return paise;
}
}
public class Shop {
public static void main(String[] args) {
Priced item = new Book(49900);
System.out.println(item.label()); // prints: 49900 paise
}
}import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
class User implements Comparable<User> {
final String name;
User(String name) {
this.name = name;
}
@Override
public int compareTo(User other) {
return this.name.compareTo(other.name);
}
public static void main(String[] args) {
List<User> users = new ArrayList<>();
users.add(new User("Cy"));
users.add(new User("Ana"));
Collections.sort(users); // uses compareTo
System.out.println(users.get(0).name); // prints: Ana
}
}Common Gotchas
Two default methods with the same signature collide
the class must override and choose. The compiler will not pick for you.
Adding an abstract method breaks implementors
prefer a `default` method when you must extend a published interface.
Interface fields are public static final
`int MAX = 10;` in an interface is a constant, not instance state.
`instanceof` an interface is about the runtime type
a proxy or lambda may implement it without being the class you expected.