What is Multiple Inheritance?
Learn multiple inheritance, the diamond problem, and how Java and C++ resolve it, with a Java default-method example.
Expected Interview Answer
Multiple inheritance is when a class inherits fields and methods from more than one parent class simultaneously, combining their behavior into a single subclass.
Languages like C++ support it directly for classes, which introduces the diamond problem: if two parent classes both inherit from a common ancestor and both override the same method, the compiler cannot unambiguously resolve which version the child should use. C++ solves ambiguity with explicit scope resolution or virtual inheritance; Java and C# avoid the problem for classes entirely by restricting inheritance to a single superclass, while still allowing a class to implement multiple interfaces, which carry no state and (in Java 8+) can provide default methods with explicit override rules for conflicts.
- Combines behavior from multiple sources into one type
- Interfaces give many of the benefits without the diamond problem
- Virtual inheritance in C++ resolves shared-ancestor ambiguity
- Encourages thinking about composition vs. inheritance tradeoffs
AI Mentor Explanation
Imagine a player is simultaneously registered under both their state academy’s training doctrine and a franchise team’s training doctrine, and both doctrines define a "field technique" for how to take a catch. If the two doctrines disagree, the player doesn’t know which technique to default to unless someone explicitly resolves which one takes precedence for that match. Multiple inheritance in code is exactly this: a class receiving conflicting behavior from two parents needs an explicit rule to pick a winner.
Step-by-Step Explanation
Step 1
Identify the parent classes
Determine which two or more classes the child needs to inherit behavior and state from.
Step 2
Check for overlapping members
Look for methods or fields with the same name/signature defined in more than one parent.
Step 3
Resolve ambiguity explicitly
Use scope resolution (C++), virtual inheritance for shared ancestors, or default-method override rules (Java interfaces) to pick a winner.
Step 4
Prefer interfaces or composition
When state conflicts are likely, favor interface inheritance or composition over multiple class inheritance.
What Interviewer Expects
- A clear definition and a real example (C++ classes vs. Java/C# interfaces)
- Explanation of the diamond problem with a concrete scenario
- Knowledge of how each language resolves or avoids the ambiguity
- Awareness of when composition is the safer alternative
Common Mistakes
- Claiming Java supports multiple inheritance for classes (it does not — only interfaces)
- Not being able to explain the diamond problem concretely
- Confusing interface implementation with class inheritance
- Missing that virtual inheritance in C++ addresses shared-ancestor duplication
Best Answer (HR Friendly)
“Multiple inheritance means a class can inherit from more than one parent class at once. It is powerful for combining behaviors, but it creates ambiguity when two parents define the same method differently — known as the diamond problem — which is why languages like Java restrict it to interfaces while C++ allows it with explicit resolution rules.”
Code Example
interface Flyer {
default String move() { return "flying"; }
}
interface Swimmer {
default String move() { return "swimming"; }
}
// Must explicitly resolve the conflicting default methods
class Duck implements Flyer, Swimmer {
@Override
public String move() {
return Flyer.super.move() + " and " + Swimmer.super.move();
}
}
// Usage
Duck duck = new Duck();
System.out.println(duck.move()); // flying and swimmingFollow-up Questions
- What is the diamond problem and how does C++ solve it with virtual inheritance?
- Why does Java disallow multiple inheritance for classes but allow it for interfaces?
- How do default methods in Java 8+ interfaces reintroduce a form of the diamond problem?
- When would you prefer composition over multiple inheritance?
MCQ Practice
1. The "diamond problem" occurs when?
The diamond problem arises when a class inherits ambiguous behavior from two parents that share a common ancestor.
2. Java avoids the diamond problem for classes by?
Java permits only single class inheritance, sidestepping most diamond ambiguity, while interfaces resolve default-method conflicts explicitly.
3. In C++, ambiguity from a shared base class is resolved using?
Virtual inheritance ensures only one shared copy of the common ancestor exists, resolving diamond-shaped ambiguity.
Flash Cards
What is multiple inheritance? — A class inheriting fields and methods from more than one parent class at once.
What is the diamond problem? — Ambiguity when two parent classes sharing a common ancestor both override the same method differently.
How does Java handle multiple inheritance? — Disallowed for classes (single inheritance only); allowed for interfaces, with explicit resolution of conflicting default methods.
How does C++ resolve diamond ambiguity? — Using virtual inheritance so the shared ancestor has only one copy in the hierarchy.