Click here to Skip to main content
15,921,884 members
Please Sign up or sign in to vote.
4.33/5 (3 votes)
See more:
Why multiple inheritance is not possible in java.
Posted
Comments
Richard MacCutchan 9-Mar-11 8:49am    
Because those are the rules, as decided by the designers of the Java language.
Sergey Alexandrovich Kryukov 9-Mar-11 16:56pm    
Yes, but why? (Actually, see my Answer.)
The question make sense and hard to answer correctly, I'll vote my 5.
--SA
Sergey Alexandrovich Kryukov 9-Mar-11 16:56pm    
Hey Teena, thank you for good Question. I voted.
--SA
Manfred Rudolf Bihy 9-Mar-11 17:10pm    
Reason for my vote of 1:
Well it could have been a good question if OP had followed advice already given. See my answer.
Sergey Alexandrovich Kryukov 9-Mar-11 18:36pm    
Manfred, I am quite satisfied, because our votes are perfect harmony: my 5 and you 1 give a well balanced summary vote :-)
--SA

Say there are two methods in class A and class B. Both has same method named 'myMethod'. Now if you inherit class A and class B to class C and then you call myMethod, whic one has to get called? class A's one or class B's one.

To avoid this confusions it is prevented multiple inheritance. But any number of interfaces are can be implemented and also multilevel inheritance is possible
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 9-Mar-11 16:54pm    
Albin, unfortunately you do not really explain why there is not multiple inheritance. One would ask: why C++ has it? The problem is much more complex and harder to explain. Also, a weak form should be mentioned.

Please look at my Answer where I try to explain that.

(Sorry, I did't vote this time.)
--SA
Albin Abel 9-Mar-11 21:57pm    
I read your answer. I agree with that
There are good reasons for that.

What your're asking about is called multiple inheritance, see this: http://en.wikipedia.org/wiki/Multiple_inheritance[^].

What Albin says does not explain the problems of multiple inheritance. This paradigm is used in C++ and other languages, is very productive but quite hard to be used in OOD. To address the problem Albin tried to point out there are two different types of inheritance: in one the same name of the member is hidden in derived class, in another one it is virtual (http://en.wikipedia.org/wiki/Virtual_inheritance[^]).

One of the problems related to OOD and OO method is well-known http://en.wikipedia.org/wiki/Diamond_problem[^]. This is not the only problem. In general, OOD required good skills in prediction of the future of development, which is very difficult to master. With multiple inheritance, this is even more difficult. On reason for lack of flexibility is that the virtual (or non-virtual) inheritance is decided once it a certain point of inheritance graphs (which is no longer a tree) and cannot be changed later, that is, being virtually inherit cannot itself be a matter or late binding. Another problem is that the virtual (or non-virtual) inheritance is applied to whole base class, not to individual members.

All that makes multiple inheritance usage difficult. As a rule of thumb, it is very good for a finally sealed set of classes. If it is designed to provide abstract classes to be sub-classes by the user, it is always a problem; it's very hard to predict what the user may want.

In contrast, in Java and .NET only a weak form of multiple inheritance is supported. A class should have strictly one base class (except Object, the base of all other classes), but any number of interfaces, and an interface can have any number of base interfaces.

—SA
 
Share this answer
 
v4
Comments
Manfred Rudolf Bihy 9-Mar-11 17:36pm    
Have my 5+!
Your advice may be lost on OP though.
Sergey Alexandrovich Kryukov 9-Mar-11 18:09pm    
Thank you. (Well most advices are lost anyway, don't you think so? Not a problem; it makes tiny difference anyway, which is enough.)
--SA
CPallini 9-Mar-11 17:43pm    
5.Anyway you forgot to mention Java (and .NET) lack of multiple inheritance really springs from their 'toy language' natures.
:-D
Sergey Alexandrovich Kryukov 9-Mar-11 18:07pm    
Thank you.

Or, I see what's you point. Isn't it C++ advocate talking?

Your note is very much biased and has more cultural roots than rational. I have very different opinion. Java and especially C# was never designed as toys (but many other languages like (any) Basic or, say, PHP are toys or worse). You're simply driven by imprinting. I have many views that would cause flame worse, but I tend to understand what can be proven in constrast to things that can be understood as cultural or personal bias.

(Just in order to irritate you a bit :-) I can mention that I am a sort of C++ opponent who can spend several hours analyzing its fallacies, and that is based on pretty good understanding of tens of different languages and paradigms. I don't do it because there is big issue involved: human culture. One more item to think about it: there is such thing as "collective decision". It can be flawed by sometimes unavoidable. I would say collective decision typically selects the worst of acceptable alternatives.)

--SA
CPallini 9-Mar-11 18:16pm    
Warning: Irritation attempt failure! :-D
I'm a little irritated Teena. As I wrote in my reply to your comment on JSOP's answer in your question here[^] I told you what exactly to google for: java multiple inheritance.
The first four hits in google carry more than enough information to explain why this was done. I would have expected that you'd come back with more than the sad repetition of your same question.

Looks like you're going onto my ignore list. Sorry!
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 9-Mar-11 17:36pm    
This time, Manfred, I'm not completely agree, even tough you're basically right. You're even more harsh today than even I am :-)

I put my such a long answer because this particular question is not so trivial; and Web search would lead to too many confusing result.

I'm trying to convince you Teena is not such a great offender so far, based on just two Question, certainly deserve a change asking some more Questions :-)

What to see a real offence? Look here (this is not about my Answers): http://www.codeproject.com/Answers/166643/Any-one-please-explain-this-code-line-by-line.aspx
and my comments - I warned OP. I have more.

Thanks for understanding.
--SA
Manfred Rudolf Bihy 9-Mar-11 17:50pm    
Well, that's your take on this SA and that is OK by me. But I'm no longer tolerating OPs blatently disregarding well given advice just to turn around and come back to ask the very same question again. Besides that I already pointed out that my advised google query had all the answers OP could wish for in the first 4 hits. Maybe I'll just keep an eye on OP to see if there was a learning effect from this.
Sergey Alexandrovich Kryukov 9-Mar-11 17:55pm    
I agree. I only say it's better to address worst cases first. I also have some ignore list and even remove posts, so I can compare.
Thank you for taking some time for this discussion.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900