Thursday 14 March 2013

Why Java does not support multiple inheritance?

Leave a Comment

Now we are sure that there is no support for multiple inheritance in java. But why? This is a design decision taken by the creators of java. The keyword is simplicity and rare use.

Simplicity

I want to share the definition for java given by James Gosling.
JAVA: A simple, object oriented, distributed, interpreted, robust, secure, architecture neutral, portable, high performance, multithreaded, dynamic language.
Look at the beauty of this definition for java. This should be the definition for a modern software language. What is the first characteristic in the language definition? It is simple.
In order to enforce simplicity should be the main reason for omitting multiple inheritance. For instance, we can considerdiamond problem of multiple inheritance.
Diamond Problem of Multiple Inheritance

We have two classes B and C inheriting from A. Assume that B and C are  overriding an inherited method and they provide their own implementation. Now D inherits from both B and C doing multiple inheritance. D should inherit that overridden method, which overridden method will be used? Will it be from B or C? Here we have an ambiguity.
In C++ there is a possibility to get into this trap though it provides alternates to solve this. In java this can never occur as there is no multiple inheritance. Here even if two interfaces are going to have same method, the implementing class will have only one method and that too will be done by the implementer. Dynamic loading of classes makes the implementation of multiple inheritance difficult.

0 comments:

Post a Comment