Single Inheritance in Java
You'll learn: Why Java allows a class to extend only one superclass, preventing multiple inheritance.
The One-Parent Rule
In Java, when you use the extends keyword, you can specify exactly one superclass—no more, no less. This is called single inheritance. Unlike some languages (like C++) that allow a class to inherit from multiple classes simultaneously, Java enforces a strict one-parent policy.
class Animal {
void breathe() {
System.out.println("Breathing...");
}
}
class Dog extends Animal {
void bark() {
System.out.println("Woof!");
}
}
// ✅ Valid: Dog has one parent (Animal)
What You Cannot Do
You cannot write something like this:
class Amphibian extends Animal, WaterCreature {
// ❌ Compile error: Java doesn't support multiple inheritance
}
Java simply does not allow a class to extend two or more superclasses directly.
Why This Limitation?
Single inheritance avoids the "diamond problem"—imagine if class C inherited from both A and B, and both A and B had a method with the same name. Which version should C inherit? Java sidesteps this ambiguity entirely by allowing only one superclass.
Think of it like biological parentage: in Java's class hierarchy, every class has exactly one biological parent (though you'll later learn about interfaces, which provide a different kind of relationship).
The Exception: Object
Every class in Java automatically extends Object if no other superclass is specified. But even then, you're still extending exactly one class—Object.
Key Takeaway:
Java enforces single inheritance: each class can extend exactly one superclass, avoiding ambiguity and keeping the inheritance hierarchy clean and simple.