In java why we are not using multiple inheritance ? what is the problem ?
------------------
suresh
In java why we are not using multiple inheritance ? what is the problem ?
------------------
suresh
hi,
u can understand by the following example y we don't have multiple inheritance..
let suppose we have two classes A & B both contain a variable i, now another class C that extends both the classes at a time want to call the variable "i" by one of its method now JVM is confused about which vaalue of "i" it should take from two which are at the same level thats why we don't have the multiple inheritance in java.
Ok manu...In C++ they handled multiple inheritance using virtual keyword. In java why they are not used the "virtual" keyword ? Why they completely remove the multiple inheritance concept?
------------------
suresh
That is probably because of its low usage. I think it is hardly used though it is available in C++.
Best practice to name a variable is to have some part of it as the name of the class. It is being followed on a large scale. With that, there won't be a need to use the keyword 'virtual'. This solves the problem.
I am not sure, GUESSING.
Lack of WILL POWER has caused more failure than
lack of INTELLIGENCE or ABILITY.
-sutnarcha-
hi suresh
When new languages come, ofcourse they come with improvement. the programmers found that MULTIPLE INHERITANCE concept in c++ is of not much use.So they removed it from java language.By not allowing multiple inheritance java simpifies the INHERITANCE MODEL. Infact multiple inheritacne adds overhead to both the complier and runtime systems.
I would like to give an eg
cosider two hierarchies
Case 1) there is a subclass C and it inherits both class A and Class B at the SAME TIME.
Case 2) B inherits class A and C inherits Class B.
Both have same effects.
I hop that with this eg ur query will b solved.
Bye
So y to go with the first one.
:)
NEVER SAY DIE.
Shenoy i am not accepting the sentence.."MULTIPLE INHERITANCE concept in c++ is of not much use".
I would like kalayama give a reply for this one.....
--------------------
suresh
In order to understand multiple inheritance, first you have to focus on the complexity , which is inherent in software system,and then you can think of why inheritance is there in java. First, java is a object oriented language in short a OOPL. The goal is reusability of software(code sharing)and of robust design. In C++ multiple inheritance is there which with bad design can increase the complexity of the code written in C++. In java that problem is removed but the advantages of the feature, that of multiple inheritance is kept through interface. Now , an abstract example can be given (which is well known)to illustrate the problem. Suppose there is a base class A and two classes x and y are derived from A which has a method aMethod() then x and y both will inherit the method. Furthermore they can override their inherited method from A. Now, if again a derived class call B is derived from x and y it will inherit the aMethod() from x and from y. If class B is instanciated and the aMethod() is called the obvious ambiguity is faced and therby leading to complexity. In order to keep the advantages of multiple inheritance java has incorporated the concept of "interface" which is nothing but type.
I hope this'll give you and idea and get you started out with the subtle and finer features of java. REad good books to probe more deeply. you'll understand.
bye
Bikgeek
In C++ multiple inheritance in not of much is not true. It's all about design. In oopl the focus is and must be on design first. Without design one can't go further with creating good quality software. Ask yourself first what is complexity and why is it inherent in software(industrial-strength-software) systems? How to manage the complexity? These are the issues one has to face. And experts say that in limited sense even in C you can achive an OO approach.
in java multiple inheritance concept(yes it is a designing concept, many language implements different way) is not removed but achived in a better way than in C++. In C++ the problem with multiple inheritance is solved through "virtual function". I am not going into detail in this space. But In java also runtime polymorphism (which is the main goal of Object Oriented Systems) is achived through "virtual function". THINK! that is required when dealing with OO.
hi suresh
I think so that u haven't got my pt!what iam trying to say is that if you have an better altenative to multiple inheritance ,then y to keep such an feature.Can u plz tell are there any programs in C++ in which using multiple inheritance is necessary. (OFCOUSE there will be many programs using multiple inheritance,but in all those v can convert them to multilevel inheritance)
bye
:)
NEVER SAY DIE.
Shenoy,
If i have five classes that is already tested and every class have a different functionality. Now i create a new class and i want to use the above classes functionality in my new class. How you achieve using multilevel inheritance?
-----------------
suresh
Hi
Suresh.P
I'm Chithambaram doing II MCA in Coimbatore Institute of Management And Technology.
In Java all the concepts are explained in Class and Objects Only,If we are use Multiple inheritence,it makes big confusion.It is a headache for programmers,so before developing the java program the C++ developers avoided the multiple inheritence.
Instead, Java's designers chose to allow multiple interface inheritance through the use of interfaces, an idea borrowed from Objective C's protocols. Multiple interface inheritance allows an object to inherit many different method signatures with the caveat that the inheriting object must implement those inherited methods.
Multiple interface inheritance still allows an object to inherit methods and to behave polymorphically on those methods. The inheriting object just doesn't get an implementation free ride. For an excellent discussion of interface inheritance, read Wm. Paul Rogers's "Reveal the Magic Behind Subtype Polymorphism"
Thank You.
By
Chithambaram.P
Nice to hear you chithambaram....
According to my previous example, how do you acheive using interface ?
In C++ they simply inherits all the classes and then used it....
But here....I expect your reply....
-----------------------
suresh
hi suresh
plz read this article CAREFULLY and i think ur doubt on Multiple inheritance will b clear.
Designing with interfaces - Java World
bye
:)
NEVER SAY DIE.
believe me !if u read this article carefully,i suppose ur query will b solved.
Bye
:)
NEVER SAY DIE.
Nice shenoy....
Once again you explain the diamond problem deeply....Thanks...
I told you i have a valuable reason why they are not using multiple inheritance.
I expect some reply about this....After i post my answer....
------------------
suresh
In most of the books they explain this diamond problem.....I already know this one...But my assumption is this is not a main reason for removing multiple inheritance....
This is one of the reason.....i accept it..
----------------
suresh
ya though java doesnt support multiple inheritance directly bt the task is accomplalished by using concept of interface in java.
Dear friends,
I got lots of reply for this question...
Thanks to all who gave reply for this question....
Here is my reason....
In java, we are using dynamic loading concept. If we use multiple inheritance then we have to load all the classes, which we will inherit in that using class. It took some time and its affect the speed of the program.
If you inherit one or two class, you can't find the timing difference. If you use more than 10 classes it will be crucial factor.
In c++ they are not using dynamic loading concept. That's why they using multiple inheritance.
I got this point from one of the IIT professor. I want to share this point. In most of the book they are not mentioned this.
Once again thanks to all......
----------------
suresh
hi suresh
i would like to thank u for sharing such an important information.
bye
:)
NEVER SAY DIE.