DEV Community

Mohammed mhanna
Mohammed mhanna

Posted on

🧬 Java Supports Single Inheritance Only — But!

If you’ve ever heard that “Java supports only single inheritance”, you might think Java is limited compared to languages like C++ that allow multiple inheritance.

But the truth is... Java gives you the best of both worlds — simplicity and flexibility — without the messy diamond problems. Let’s break it down 👇


đź§© What Is Inheritance in Java?

Inheritance allows one class to acquire the properties and behaviors (fields and methods) of another.

It’s one of the core pillars of Object-Oriented Programming (OOP).

class Animal {
    void eat() {
        System.out.println("Eating...");
    }
}

class Dog extends Animal {
    void bark() {
        System.out.println("Barking...");
    }
}

public class Main {
    public static void main(String[] args) {
        Dog d = new Dog();
        d.eat();  // Inherited
        d.bark(); // Own behavior
    }
}
Enter fullscreen mode Exit fullscreen mode

âś… Dog inherits from Animal.
âś… We get code reuse and a clear hierarchy.


đźš« Why Only Single Inheritance?

In Java, a class cannot inherit from more than one class:

class A {}
class B {}
class C extends A, B {} // ❌ Compilation error
Enter fullscreen mode Exit fullscreen mode

Reason?
To avoid ambiguity and complexity that come from multiple inheritance.

Imagine both A and B have a method show().
If C inherits from both, which show() should it call?

That’s the famous “Diamond Problem” 💎


💡 But Wait — Java Isn’t Limited!

Even though Java restricts multiple inheritance for classes,
it allows multiple inheritance through interfaces âś…


⚙️ Example: Multiple Inheritance via Interfaces

interface CanFly {
    void fly();
}

interface CanSwim {
    void swim();
}

class Duck implements CanFly, CanSwim {
    public void fly() {
        System.out.println("Flying...");
    }

    public void swim() {
        System.out.println("Swimming...");
    }
}

public class Main {
    public static void main(String[] args) {
        Duck d = new Duck();
        d.fly();
        d.swim();
    }
}
Enter fullscreen mode Exit fullscreen mode

âś… A class can implement multiple interfaces
✅ Avoids ambiguity because interfaces only declare methods — they don’t provide full implementations (unless using default methods, handled explicitly).


đź§  Why Interfaces Solve the Problem

Interfaces give Java a way to provide multiple behavior types safely:

They specify what a class should do, not how.

The implementing class defines the actual behavior.

No confusion — no diamond problem.

And since Java 8, interfaces can also have default methods,
but if there’s a conflict, the implementing class must override it — giving full control to the developer.


Feature Multiple Inheritance Allowed? Example
Classes ❌ No class C extends A, B (not allowed)
Interfaces âś… Yes class C implements X, Y
Mix (Class + Interfaces) âś… Yes class C extends A implements X, Y

đź§­ The Real Takeaway

Java chose clarity over chaos.
You still get multiple behaviors through interfaces,
but without the dangers of conflicting implementations.

👉 So yes — Java supports single inheritance of classes,
but multiple inheritance of type definitions (interfaces).


đź’¬ Final Thought

Think of it this way:

Java lets you inherit structure once,
but inherit abilities many times. đź’Ş


❓Question for You

Do you prefer using interfaces for flexibility or abstract classes for structured hierarchies?
Share your thoughts in the comments 👇

Top comments (2)

Collapse
 
roshan_sharma_7deae5e0742 profile image
roshan sharma

I usually go with interfaces for flexibility. They let you mix different behaviors without getting tangled in inheritance issues. Abstract classes are good if you need a solid base with shared code, but for most cases, interfaces just make life easier.

Collapse
 
mohamad_mhana profile image
Mohammed mhanna

Exactly! Interfaces usually keep designs simpler and more scalable.