A class in Java can extend multiple classes
True
False
The Correct Answer and Explanation is:
Answer: False.
In Java, a class is permitted to extend only one other class. This is known as single inheritance. When you write
public class SubClass extends SuperClass {
// ...
}
SubClass inherits the methods and fields of exactly one SuperClass. You cannot write:
public class SubClass extends SuperClassA, SuperClassB {
// INVALID in Java!
}
Attempting to extend more than one class will result in a compile-time error.
Why Java Chooses Single Inheritance
- Avoiding the Diamond Problem
In languages that allow multiple inheritance (like C++), the so‑called “diamond problem” can arise. SupposeClassAandClassBboth extendClassX, andClassCextends bothClassAandClassB. IfClassXdefines a methodfoo(), which version shouldClassCinherit? Ambiguity about which superclass’s implementation to use can lead to subtle bugs. Java sidesteps this entire class‑based ambiguity by forbidding multiple class inheritance. - Simplicity and Maintainability
With single inheritance, the class hierarchy remains a straightforward tree. This simplification makes it easier to reason about which methods and fields a subclass will inherit, and how to override them. Tooling like IDE refactoring, debugging, and code analysis also become more robust when there’s a single, clear lineage for each class.
How Java Supports Multiple “Types”
Though Java forbids extending multiple classes, it allows a form of multiple inheritance of interfaces. An interface in Java is a pure specification—a contract of method signatures (and since Java 8, default and static methods) without full implementation. A class can implement any number of interfaces:
public interface Flyer {
void fly();
}
public interface Swimmer {
void swim();
}
public class Duck extends Animal implements Flyer, Swimmer {
@Override
public void fly() { /* … */ }
@Override
public void swim() { /* … */ }
}
Here, Duck gains a single‑inheritance relationship from Animal (a class) and “multiple inheritance” of behavior contracts from Flyer and Swimmer.
Key Takeaways
- Extends = single class only.
- Implements = any number of interfaces.
- This design avoids method-resolution ambiguities, keeps hierarchies simple, and still provides flexibility through interfaces. Hence, it is false that a Java class can extend multiple classes.