1. The Hook (The "Byte-Sized" Intro)
In a Nutshell: Java doesn't allow multiple inheritance for classes (one parent only), but DOES allow multiple inheritance through interfaces. A class can implement multiple interfaces, inheriting the contracts of all of them. This solves the Diamond Problem while enabling flexible, capability-based design.
Think of a smartphone. It "implements" multiple capabilities: Camera, Phone, MusicPlayer, GPS. It doesn't inherit from one super-device—it combines multiple independent capabilities!
2. Conceptual Clarity (The "Simple" Tier)
💡 The Analogy: The Multi-Tool
A Swiss Army knife:
- Implements multiple tools: Knife, Scissors, Screwdriver, BottleOpener
- Each tool is independent (interface)
- The knife combines them all
You can't inherit from multiple parent knives, but you can implement multiple "tool" interfaces!
Hand-Drawn Logic Map
3. Technical Mastery (The "Deep Dive")
Formal Definition
Multiple Inheritance via Interfaces:
- Syntax:
class MyClass implements Interface1, Interface2, Interface3 - The class MUST implement all methods from all interfaces
- If two interfaces have the same method signature, one implementation satisfies both
Diamond Problem: When two parents have the same method, which one does the child inherit? Java solves this:
- Classes: Forbids multiple inheritance (no diamond problem)
- Interfaces: Allows it but requires explicit resolution for default methods
The "Why" Paragraph
Why allow it for interfaces but not classes? The Diamond Problem with classes causes ambiguity in state (which parent's fields?) and behavior (which parent's implementation?). With interfaces (pre-Java 8), there was no implementation to conflict! Java 8 added default methods, creating potential conflicts, but Java forces you to explicitly resolve them.
Visual Architecture: Diamond Problem
4. Interactive & Applied Code
The "Perfect" Code Block
interface Flyable {
void fly();
default void move() {
System.out.println("Moving through air");
}
}
interface Swimmable {
void swim();
default void move() {
System.out.println("Moving through water");
}
}
interface Walkable {
void walk();
}
// Implementing multiple interfaces
class Duck implements Flyable, Swimmable, Walkable {
@Override
public void fly() {
System.out.println("Duck flying");
}
@Override
public void swim() {
System.out.println("Duck swimming");
}
@Override
public void walk() {
System.out.println("Duck walking");
}
// MUST resolve default method conflict!
@Override
public void move() {
System.out.println("Duck moving (custom)");
// Or call specific interface: Flyable.super.move();
}
}
public class Main {
public static void main(String[] args) {
Duck duck = new Duck();
duck.fly();
duck.swim();
duck.walk();
duck.move();
// Polymorphism with multiple types
Flyable flyingAnimal = duck;
flyingAnimal.fly();
Swimmable swimmer = duck;
swimmer.swim();
}
}The "Anti-Pattern" Example
❌ Not Resolving Default Method Conflicts
interface A {
default void show() { System.out.println("A"); }
}
interface B {
default void show() { System.out.println("B"); }
}
class C implements A, B {
// ❌ ERROR: Must override show() to resolve conflict!
}✅ Fix:
class C implements A, B {
@Override
public void show() {
A.super.show(); // Call A's version explicitly
}
}5. The Comparison & Decision Layer
Versus Table: Class Inheritance vs. Interface Implementation
| Feature | extends (Class) | implements (Interface) |
|---|---|---|
| Count | Single only | Multiple allowed |
| Diamond Problem | Avoided (not allowed) | Resolved via rules |
| Relationship | "is-a" | "can-do" (capability) |
6. The "Interview Corner" (The Edge)
The "Killer" Interview Question: "Why does Java allow multiple inheritance for interfaces but not classes?" Answer:
- Classes: Have state (fields) and implementation. Multiple inheritance would cause ambiguity: which parent's field? Which parent's method implementation?
- Interfaces (originally): Had NO state and NO implementation—just contracts. No ambiguity!
- Java 8+: Default methods created potential conflicts, but Java forces explicit resolution via
InterfaceName.super.method().
Real-World Example
class ArrayList implements List, RandomAccess, Cloneable, Serializable {
// Implements 4 interfaces! Each adds a capability.
}Pro-Tip: Use interface-based design for flexibility:
// ❌ Rigid
ArrayList<String> list = new ArrayList<>();
// ✅ Flexible
List<String> list = new ArrayList<>();This lets you swap ArrayList for LinkedList without changing the type!