Search This Blog

Monday 15 September 2014

Why Multiple Inheritance is not supported in Java.?

Inheritance:  When an object or class is based on another object or class, using the same implementation (inheriting from a class) or specifying implementation to maintain the same behavior (realizing an interface; inheriting behavior). It is a mechanism for code reuse and to allow independent extensions of the original software via public classes and interfaces. The relationships of objects or classes through inheritance give rise to a hierarchy.


1:  class SuperClass{  
2:    void superClassMethod(){  
3:      //Code  
4:    }  
5:  }  
6:  class SubClass extends SuperClass{  
7:    void subClassMethod(){  
8:      //Code  
9:    }  
10:  }  

In the above code, SubClass extends SuperClass and inherits the properties of SuperClass.

A class basically has following:
1. State
2. Field

Dogs have state (name, color, breed, hungry) and behavior (barking, fetching, wagging tail). 
Bicycles also have state (current gear, current pedal cadence, current speed) and behavior (changing gear, changing pedal cadence, applying brakes). Identifying the state and behavior for real-world objects is a great way to begin thinking in terms of object-oriented programming.

Now suppose you are able to inherit more than one class in Java.
Classes-
1. Animal
2. Mammal
3. Bird
4. Dog
Mammal and Bird inherited Animal class which is possible in Java. A class can be inherited by any number of classes if it allowed to be inherited. Dog class is actually a animal (is-a relationship). 

Dog can extend only one class according to Java Language specifications, which means a Dog can acquire the properties of only one class directly. 
Now suppose if your class Dog is capable of inheriting two classes- Mammal and Bird at a time. And when you instantiate a Dog class, the object of Dog will inherit the state and behavior of all the extended superclasses.  Just imagine this example in real world, what if you see a Dog who is flying in above you?

This problem of inheriting properties of more than one class at a time is also known as Diamond Problem
As in the above figure on left you can see, Animal class is extended by two classes - Mammal and Bird which is possible. Now the a dog class extending two classes - Mammal and Bird.

Method and constructor of superclasses accessing/defining the same field as method and constructor of subclasses. Which method or constructor will take precedence? We can't decide the precedence.
This is the reason why multiple inheritance is not there in Java.

There are people out there who say- Interfaces are meant to achieve multiple inheritance. Interfaces is not actually meant for achieving multiple inheritance.
But yes, we can simulate the behaviour of Multiple Inheritance using interface.
Because interfaces do not contain fields and because of this you do not have to worry about problems that result from multiple inheritance of state.

No comments:

Post a Comment

Thanks for feedback,
Your opinion will surely help me to enhance the quality of the content.