Classes need a way to define behavioral contracts without dictating implementation. Without interfaces, achieving polymorphism across unrelated class hierarchies would require a common abstract superclass — forcing artificial inheritance relationships and preventing multiple type identities.
An interface in Java is a reference type that defines a set of abstract method signatures (a contract) that implementing classes must fulfill. Unlike classes, interfaces support multiple inheritance — a class can implement multiple interfaces. Java 8+ added default methods (with body) and static methods in interfaces.
When a class declares implements InterfaceName, the compiler checks that the class provides implementations for all abstract interface methods. At runtime, interface method dispatch uses the itable (interface method table), which is resolved differently from the vtable. A class can implement multiple interfaces, each defining a distinct role.
- Multiple inheritance: A class can implement many interfaces
- All methods are public: Interface methods are implicitly
public abstract - Default methods (Java 8+): Methods with a body in interfaces, enabling backward-compatible evolution
- Functional interfaces: Interfaces with exactly one abstract method — target for lambda expressions
- Built from: Java Abstraction — interfaces are a form of full abstraction
- Builds into: Java Polymorphism — interfaces enable polymorphic behavior across unrelated hierarchies
- Builds into: Lambda Expressions & Streams — functional interfaces are the target type for lambdas
- Contrasts with: Java Inheritance — single vs multiple inheritance; interface vs class
- Default method diamond problem: If two interfaces define the same default method, the class must override
- Interface constants: Fields in interfaces are implicitly
public static final - FunctionalInterface annotation:
@FunctionalInterfaceis a documentation aid — the compiler validates single abstract method - Sealed interfaces (Java 17+): Restrict which classes can implement an interface