Multiple Interfaces
& Default Methods
Learn how Java classes can implement multiple interfaces, and discover how default methods bring behavior to interfaces without breaking existing code.
What Is an Interface?
An interface in Java is a contract: it declares method signatures that any implementing class must provide. Interfaces define what an object can do without specifying how it does it.
Why Multiple Interfaces?
Java forbids multiple class inheritance to avoid the "diamond problem." But a class can implement many interfaces, gaining multiple capabilities without ambiguity in state.
Visualizing the Structure
The Syntax Rule
Use the implements keyword followed by a comma-separated list of interface names. The class must then provide concrete bodies for every abstract method from all listed interfaces.
Enter Default Methods
Since Java 8, interfaces can include methods with a body using the default keyword. This lets you add new methods to an interface without forcing every existing class to implement them.
Interface vs Abstract Class
| Interface | Abstract Class | |
|---|---|---|
| Multiple inheritance | ✓ Yes, many | ✕ Only one |
| Instance fields | ✕ No (constants only) | ✓ Yes |
| Constructors | ✕ No | ✓ Yes |
| Default methods | ✓ Since Java 8 | ✓ Always |
| Access modifiers | Methods are public by default | Members can have any modifier |
The Diamond Problem
When two interfaces provide a default method with the same signature, the implementing class faces a conflict. Java requires you to explicitly override the method to resolve it.
Resolving Conflicts
How to Apply It
Example 1: Basic Multi-Interface
Example 2: Default Methods in Action
Example 3: Conflict Resolution
Polymorphism with Interfaces
When a class implements multiple interfaces, you can reference it through any of those interface types. This is one of the most powerful features of Java's type system.
Common Mistakes
Summary Table
| Concept | Key Rule | Example |
|---|---|---|
| Multiple Interfaces | implements A, B, C | class Duck implements Flyable, Swimmable |
| Default Method | default keyword in interface | default void land() { ... } |
| Conflict Resolution | Override + InterfaceName.super | Human.super.greet() |
| Abstract Method | Must be implemented by class | void fly(); (no body) |
| Polymorphic Reference | Reference type limits visibility | Printable p = new AllInOnePrinter() |
Question 1 of 5
Which keyword does a Java class use to adopt an interface?
Question 2 of 5
How many interfaces can a single Java class implement?
Question 3 of 5
What happens if two interfaces define a default method with the same signature and a class implements both?
Question 4 of 5
Given the code below, what does Cyborg.greet() print?
interface Robot { default void greet() { System.out.println("Beep!"); } }
interface Human { default void greet() { System.out.println("Hello!"); } }
class Cyborg implements Robot, Human {
public void greet() { Robot.super.greet(); }
}
Question 5 of 5
Which statement about default methods is false?