Why a class can’t extend multiple classes

Reading Time: < 1 minute

In Java, a class can’t extend multiple classes. Why do you think this is so? Let’s examine
this issue using an example, in which the class Programmer is allowed to inherit two
classes: Employee and Philanthropist. Figure 6.9 shows the relationship between
these classes and the corresponding code.


If class Programmer inherited the method receiveSalary, defined in both Employee
and Philanthropist, what do you think a Programmer would do with his or her salary:
pay dues (like an Employee) or donate it (like a Philanthropist)? What do you think
would be the output of the following code?

class Test {
public static void main(String args[]) {
Programmer p = new Programmer();

Would this print
“PayDues” or “Donate”?

In this case, class Programmer can access two receiveSalary methods with identical
method signatures but different implementations, so it is impossible to resolve
this method call. This is why classes aren’t allowed to inherit multiple classes in Java.

Because a derived class may inherit different implementations for
the same method signature from multiple base classes, multiple inheritance is
not allowed in Java.