Back to Inheritance
Q1Question 1 of 3

Can Java achieve multiple inheritance?

Key Concepts
  • Java does not allow a class to extend more than one class, preventing the diamond problem
  • Diamond problem: if A and B both override a method from C, which version does X (extending both) inherit?
  • Java allows multiple interface implementation: class X implements A, B is valid
  • If two interfaces have conflicting default methods, the implementing class must override to resolve explicitly
  • Resolution priority: class method wins over interface default; sub-interface default wins over super-interface default
  • InterfaceName.super.method() syntax lets the class delegate to a specific interface's default method

Definition

No multiple class inheritance (diamond problem: ambiguous method resolution). Yes, multiple interface implementation. If two interfaces have the same default method, the implementing class must override to resolve the conflict explicitly.

Java does not allow a class to extend more than one class to prevent the diamond problem.

  • Diamond problem: if class A and class B both override a method from class C, and class X extends both A and B, the compiler cannot decide which version X inherits.
  • Java's solution: a class can implement multiple interfaces. If both interfaces have abstract methods with the same signature, the class provides one implementation satisfying both.
  • Default method conflict: if two interfaces provide the same default method, the compiler forces the class to override and explicitly resolve the conflict.
  • Resolution priority: class method always wins over interface defaults. Sub-interface default wins over super-interface default.
  • Delegation syntax: InterfaceName.super.method() lets the implementing class call a specific interface's default method.

Imagine Rajesh at National Bank of India works in both Trading and Compliance. Both departments have processEndOfDay(). Trading says close all open positions. Compliance says generate regulatory report. If Rajesh inherits both, which one does he follow? Ambiguous. Java prevents this by saying you can only belong to one department (single class inheritance). But you can hold multiple licenses (interfaces): Trading License and Compliance License. Rajesh implements both, and he decides how to do each one.

Understanding the Concepts

Can Java achieve multiple inheritance?

Java disallows multiple class inheritance but allows multiple interface implementation.

What is the diamond problem?

JAVA
// Hypothetical: if Java allowed multiple class inheritance
class Vehicle {
    public void start() { System.out.println("Vehicle starting"); }
}
class ElectricVehicle extends Vehicle {
    @Override public void start() { System.out.println("Electric motor starting silently"); }
}
class GasVehicle extends Vehicle {
    @Override public void start() { System.out.println("Gas engine starting with roar"); }
}
// ILLEGAL in Java:
// class HybridVehicle extends ElectricVehicle, GasVehicle { }
// h.start(); // which start()? Electric's silent? Gas's roar? Ambiguous.
//
//        Vehicle
//       /      \
// Electric    Gas
//       \      /
//       Hybrid    <- which start()?

How do interfaces solve this?

JAVA
package com.bank.service;

import java.math.BigDecimal;

public interface Tradable {
    void executeTrade();
    default String getMarket() { return "NSE"; }
}

public interface Reportable {
    void generateReport();
    default String getMarket() { return "BSE"; } // same default method signature
}

public class TradingService implements Tradable, Reportable {
    @Override public void executeTrade() { System.out.println("Trade executed"); }
    @Override public void generateReport() { System.out.println("Report generated"); }

    // Conflict: both interfaces have getMarket() default
    // Compile error if not overridden: "inherits unrelated defaults for getMarket()"
    @Override
    public String getMarket() {
        return Tradable.super.getMarket(); // delegate to Tradable's version: "NSE"
        // Or: return Reportable.super.getMarket(); // delegate to Reportable's: "BSE"
        // Or: return "NSE + BSE"; // own implementation
    }
}

What is the resolution priority?

JAVA
// Priority: class method > sub-interface default > super-interface default

public interface Auditable {
    default String audit() { return "Interface audit"; }
}

public class BaseService {
    public String audit() { return "Class audit"; }
}

public class TradingService extends BaseService implements Auditable {
    // No override needed. BaseService.audit() wins over Auditable.audit().
}

new TradingService().audit(); // "Class audit" (class wins)

Why do interfaces avoid the diamond problem?

  • Abstract methods have no body: no ambiguity (class provides the body).
  • Default methods: if conflict, compile error forces class to resolve.
  • Static methods: not inherited, no conflict possible.
  • Class always wins: if class has the method, it overrides all interface defaults.

What should a senior engineer know?

  • Java's approach is safe: conflicts are always resolved explicitly by the programmer.
  • The InterfaceName.super.method() syntax only works inside the implementing class.
  • Sealed interfaces (Java 17) further control which classes can implement them.

Visual Overview

DIAGRAM
  DIAMOND PROBLEM (disallowed)
       Vehicle
      /       \
  Electric   Gas
      \       /
      Hybrid        <- AMBIGUOUS: which start()?

  JAVA SOLUTION (interfaces)
  ┌──────────────────────────────┐
  │ TradingService                │
  │   implements Tradable,        │
  │              Reportable       │
  │                               │
  │ Both have getMarket() default │
  │ Class MUST override:          │
  │   Tradable.super.getMarket()  │
  │   Reportable.super.getMarket()│
  │   or own implementation       │
  └──────────────────────────────┘

  Resolution priority:
  1. Class method (always wins)
  2. Sub-interface default
  3. Super-interface default

Key Takeaway

Java disallows multiple class inheritance to prevent the diamond problem: if two parent classes both override the same method, the compiler cannot decide which version the child inherits. Java allows multiple interface implementation instead. If two interfaces have abstract methods with the same signature, the class provides one implementation satisfying both. If two interfaces have conflicting default methods, the compiler forces the class to override and explicitly resolve the conflict using InterfaceName.super.method() syntax.

The resolution priority is: class method always wins over interface defaults, sub-interface default wins over super-interface default, and two unrelated interface defaults require the implementing class to override.

Remember one sentence: Java prevents the diamond problem by disallowing multiple class inheritance and requiring the implementing class to explicitly resolve any conflicting interface default methods.

---

Share
Use ← → arrow keys to navigate