What is Multiple Inheritance in Java with Example

Multiple inheritance is a concept where a class inherits behavior from more than one parent class. While some languages support this directly, Java deliberately does not allow a class to extend more than one class at a time.

Why Java Avoids Multiple Class Inheritance

The main reason is the "Diamond Problem" — if two parent classes have a method with the same signature, the compiler wouldn't know which version the child class should inherit, leading to ambiguity.

How Java Achieves Multiple Inheritance Safely

  • Interfaces: a class can implement multiple interfaces, inheriting their method contracts without ambiguity
  • Default Methods: since Java 8, interfaces can have default method implementations, and conflicts must be explicitly resolved by the implementing class
  • Composition: combining behavior from multiple classes by including their objects as fields, instead of inheriting directly
This is a favorite Java interview question — being able to explain the Diamond Problem and how interfaces solve it shows real conceptual understanding, not just memorized syntax.

Master Java with Uncodemy

Hands-on training, live projects, and placement support in our Java Programming Course.

Explore the Course

Implementing Several Interfaces in Practice

A class can implement any number of interfaces, and each one adds a role the class promises to fulfil. Here a smartphone acts as both a camera and a music player:

interface Camera {
    void takePhoto();
}

interface MusicPlayer {
    void play(String song);
}

class SmartPhone implements Camera, MusicPlayer {
    @Override
    public void takePhoto() {
        System.out.println("Photo saved");
    }

    @Override
    public void play(String song) {
        System.out.println("Playing " + song);
    }
}

Camera c = new SmartPhone();      // usable wherever a Camera is expected
MusicPlayer m = new SmartPhone(); // and wherever a MusicPlayer is expected

This is the real benefit: one object can be passed to code that only knows about cameras and to code that only knows about music players, without either side depending on the concrete class.

Resolving the Diamond Problem With Default Methods

Default methods brought back a small version of the diamond problem. If two interfaces provide a default method with the same signature, the class will not compile until you override it. Inside the override, you can call either parent version with the InterfaceName.super.method() syntax.

interface Camera {
    default String start() { return "Camera ready"; }
}

interface Phone {
    default String start() { return "Phone ready"; }
}

class SmartPhone implements Camera, Phone {
    @Override
    public String start() {
        return Camera.super.start() + " and " + Phone.super.start();
    }
}

System.out.println(new SmartPhone().start());
// Camera ready and Phone ready

Java follows three simple rules when it looks for a method:

  1. Classes win. A method defined in the class or any superclass beats an interface default.
  2. The more specific interface wins. If one interface extends another, its default takes priority.
  3. Otherwise, you decide. If neither rule settles it, the class must override the method.

Interfaces cannot hold instance fields, only constants, so there is never confusion over which copy of a variable an object owns. That is the deeper reason Java's approach stays safe.

Composition as the Everyday Alternative

When you want to reuse working code from several classes, composition is often cleaner than inheritance. The class holds objects and delegates to them:

class Engine { void start() { System.out.println("Engine on"); } }
class Radio  { void tune(int fm) { System.out.println("Tuned to " + fm); } }

class Car {
    private final Engine engine = new Engine();
    private final Radio radio = new Radio();

    void drive() {
        engine.start();
        radio.tune(98);
    }
}

Composition lets you swap parts, test them separately and avoid fragile class hierarchies. A useful phrase in interviews is "a car has an engine; it is not an engine".

The two ideas also combine well. A common pattern is to define the role as an interface, such as Notifier, and give a class a field of that interface type. The class can then work with an email notifier today and a text-message notifier tomorrow, without changing its own code or its parent class.

Interview Tips and Common Misunderstandings

  • Saying Java has "no multiple inheritance at all" is incomplete: it has multiple inheritance of type through interfaces, but not of state through classes.
  • An interface may extend several other interfaces, for example interface SmartDevice extends Camera, Phone.
  • Abstract classes still allow only single inheritance, even if they contain no fields.
  • When answering, give the diamond example, show the X.super.method() fix and finish with when you would choose composition.