01 / 21

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.

Java

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.

public interface Drawable { void draw(); void resize(int factor); }

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.

✕ Class Inheritance
A class can extend only one superclass. Multiple inheritance of state is prohibited.
✓ Interface Implementation
A class can implement many interfaces. Each adds a new behavioral contract.

Visualizing the Structure

«interface» Flyable «interface» Swimmable «interface» Walkable Duck implements all three Dashed arrows = "implements" relationship

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.

public class Duck implements Flyable, Swimmable, Walkable { @Override public void fly() { /* flap wings */ } @Override public void swim() { /* paddle */ } @Override public void walk() { /* waddle */ } }

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.

public interface Flyable { void fly(); default void land() { System.out.println("Gliding to a stop..."); } }

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.

«interface» InterfaceA «interface» InterfaceB default greet() default greet() MyClass Conflict! Must override greet() to resolve

Resolving Conflicts

1
Override the conflicting method in the implementing class to provide your own body.
2
Delegate to a specific interface using the syntax: InterfaceName.super.method()
3
Write a completely new implementation that ignores both defaults.
public class MyClass implements InterfaceA, InterfaceB { @Override public void greet() { // Option: delegate to InterfaceA's version InterfaceA.super.greet(); } }

How to Apply It

1
Identify behaviors: list every capability your class needs (e.g., serializable, comparable, drawable).
2
Create or find interfaces: write one interface per behavior, each with focused method declarations.
3
Use default methods wisely: add default implementations for common behavior most classes will share.
4
Implement all interfaces: use implements A, B, C and provide bodies for every abstract method.
5
Resolve conflicts: if two interfaces share a default method signature, override it explicitly.

Example 1: Basic Multi-Interface

interface Printable { void print(); } interface Scannable { void scan(); } public class AllInOnePrinter implements Printable, Scannable { @Override public void print() { System.out.println("Printing document..."); } @Override public void scan() { System.out.println("Scanning document..."); } }

Example 2: Default Methods in Action

interface Logger { default void log(String msg) { System.out.println("LOG: " + msg); } } interface Timestamped { default String timestamp() { return java.time.LocalTime.now().toString(); } } public class EventTracker implements Logger, Timestamped { public void track(String event) { log(timestamp() + " - " + event); } }

Example 3: Conflict Resolution

interface Robot { default void greet() { System.out.println("Beep boop!"); } } interface Human { default void greet() { System.out.println("Hello there!"); } } public class Cyborg implements Robot, Human { @Override public void greet() { Human.super.greet(); // chooses Human's version } }

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.

AllInOnePrinter device = new AllInOnePrinter(); // Reference as Printable Printable p = device; p.print(); // OK // Reference as Scannable Scannable s = device; s.scan(); // OK // p.scan() would NOT compile // because Printable has no scan()

Common Mistakes

✕ Forgetting an Override
Not implementing all abstract methods from every interface causes a compile error.
✕ Ignoring Conflicts
If two interfaces have the same default method, you must override it: the compiler will not guess.
✕ Using extends for Interfaces
Classes implement interfaces. The keyword extends is only for class-to-class or interface-to-interface.

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?

Question 5 of 5

Which statement about default methods is false?