Menu

Showing posts with label Multiple inheritance. Show all posts
Showing posts with label Multiple inheritance. Show all posts

Why Java doesn't support multiple inheritance?

Java doesn't support multiple inheritance because of deadlock and ambiguity situation.

A class cannot extend two classes, but a class can implement multiple interfaces. So, we use interfaces to achieve multiple inheritance in Java programming.

e.g. Class C extends A, B { //statements }

The above class declaration is invalid in Java programming. but possible in various other programming languages.

e.g. Class C extends A implements D, F { //codes }

The above class declaration is valid in Java. In the above class declaration, we have inherited class A and implementing two interfaces D and F. This way we can implement multiple interfaces in Java.

Example Class C extends Class A and Class B and both classes A and B have evaluate() method. When class C tries to inherit evaluate method then the compiler will get confused and unable to decide which evaluate method to inherit, either from class A or B. So to remove this ambiguity from the program Java doesn't support multiple inheritance.