Multiple Inheritance Through Interfaces

Naveen AnamaneniUpdated · 5 min read

A smartphone is a phone, a camera and a music player at the same time. A Java class can extend only one class, but it can implement as many interfaces as it needs. That's how Java offers multiple inheritance of type: one object can be used wherever any of its interfaces is expected, without the diamond problem that multiple class inheritance would bring.

One Class, Several Interfaces

One object, used through three types
Gadgets.java▶ Try it
public class Gadgets {
    interface Phone { String call(String who); }
    interface Camera { String click(); }
    interface MusicPlayer { String play(String song); }
 
    static class Smartphone implements Phone, Camera, MusicPlayer {
        public String call(String who) { return "Calling " + who; }
        public String click() { return "Photo saved"; }
        public String play(String song) { return "Playing " + song; }
    }
 
    static void ring(Phone p) { System.out.println(p.call("Srihitha")); }
    static void shoot(Camera c) { System.out.println(c.click()); }
 
    public static void main(String[] args) {
        Smartphone mine = new Smartphone();
        ring(mine);
        shoot(mine);
        MusicPlayer player = mine;
        System.out.println(player.play("Navika's playlist"));
        System.out.println(mine instanceof Camera);
    }
}
Output
Calling Srihitha
Photo saved
Playing Navika's playlist
true

How it works

  1. Smartphone implements Phone, Camera, MusicPlayer: one class signs three contracts, separated by commas.
  2. The same object is passed to ring as a Phone and to shoot as a Camera. Each method sees only the part of the object it needs.
  3. mine instanceof Camera is true: the object is a Camera, as well as a Phone and a MusicPlayer.

Try it yourself: add an interface Torch with String lightOn() and make Smartphone implement it too.

Interfaces Extending Interfaces

An interface can extend several interfaces at once, combining their contracts:

Combined.java▶ Try it
public class Combined {
    interface Readable { String read(); }
    interface Writable { void write(String text); }
    interface Notebook extends Readable, Writable { int pages(); }
 
    static class SchoolNotebook implements Notebook {
        private final StringBuilder text = new StringBuilder();
        public String read() { return text.toString(); }
        public void write(String t) { text.append(t); }
        public int pages() { return 100; }
    }
 
    public static void main(String[] args) {
        Notebook book = new SchoolNotebook();
        book.write("Advitha's science notes");
        System.out.println(book.read() + ", " + book.pages() + " pages");
    }
}
Output
Advitha's science notes, 100 pages

Notebook extends Readable and Writable, so a class implementing Notebook must provide all three methods.

DeclarationHow many parents
class A extends BOne class
class A implements X, YAny number of interfaces
class A extends B implements X, YOne class plus any number of interfaces
interface X extends Y, ZAny number of interfaces (interfaces never implement)

Why No Diamond Problem

Interfaces carry no instance fields, so a class can never end up with two copies of the same state. Abstract methods with the same signature from two interfaces simply merge: the class implements the method once and satisfies both. Only default methods with code can clash, and Java then refuses to compile until the class decides (see "Diamond Problem with Default Methods").

Common Mistakes

Writing implements before extends. The order is fixed: class C extends B implements X.

Using implements between interfaces. An interface extends other interfaces.

Building one giant interface. Classes then have to implement methods they don't need.

Interview Corner

Does Java support multiple inheritance?

Not of classes: a class has exactly one superclass. It supports multiple inheritance of type through interfaces: a class can implement many interfaces and an interface can extend many interfaces. Since Java 8, default methods also allow inheriting behavior from several interfaces, with compile-time rules that force the class to resolve any conflict.

What happens if two interfaces declare the same abstract method?

Nothing problematic. The implementing class provides one method with that signature, and it satisfies both interfaces. A conflict arises only if both supply default implementations, or if the return types are incompatible.

Practice

  1. Easy: Create interfaces Swimmer and Runner and a class Triathlete that implements both. Pass the same object to a method that takes a Swimmer and another that takes a Runner.
  2. Harder: Create interface Account extends Depositable, Withdrawable and a SavingsAccount that implements it. Show that a SavingsAccount can be passed where any of the three types is expected.

Quick check: Multiple inheritance through interfaces

4 questions
1How many interfaces can a class implement?
2Which declaration is valid?
3Two interfaces declare String name();. The class must:
4Why can't interfaces cause the "two copies of a field" problem?

Key Takeaways

Was this tutorial helpful?

Lesson 135 of 482