• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

multiple inheritance

 
yashendra chandrakar
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
why is it that we can achieve multiple inheritance by implementing interfaces but not by extending.(i.e. what's logic behind not allowing multiple inheritance through extending classes?)
thanks in advance
Yashendra.
 
Anthony Villanueva
Ranch Hand
Posts: 1055
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Coming from a C++ background, I think can answer this question.
Suppose I have two classes A and B, both of which contain an int instance variable named i. Now, suppose we have a class C which subclasses both. Naturally, C inherits both A::i and B::i. Having two distinct instance variables with the same name is asking for a lot of extra caution. Also imagine inheriting two methods (A::f() and B::f())with identical signatures but different implementations.
Of course it is possible to force the issue by explicitly invoking the class scope, like A::f(), but it limits the possibilities of polymorphism.
Multiple implementation inheritance by itself is not necessarily bad; there are certainly pros and cons for it, in the same way that Java has a convenient garbage collecting thread while C++ objects on the heap must be manually accounted for. Go for convenience or go for user-configurable memory management? C++ is high-powered because of its many features, but along with it was the added complexity and margin for (sometimes spectacular) error. The fathers of Java felt it was too high a price to pay.
-anthony

It just so happened that the fathers of Java felt that
 
Roseanne Zhang
Ranch Hand
Posts: 1953
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
C# does not support multi inheritance either. Java and C# are both considered more OO than C++.
Need Java real project experience? Join our project team here!
 
Greg Brouelette
Ranch Hand
Posts: 144
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Interestingly enough, Bjarne Stroustrup, then author of the C++ language, has said that if he had to do it all over again, he would have left multiple inheiritence out of C++.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Roseanne Zhang:
C# does not support multi inheritance either. Java and C# are both considered more OO than C++.

By whom? And why? Certainly not because of lack of multiple inheritance...
 
Anthony Villanueva
Ranch Hand
Posts: 1055
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Well, IMO I really wouldn't say that Java is more OO than C++. C++ supports the usual elements of OOP: abstraction, encapsulation, class hierarchies, polymorphism, typing and object persistence.
Well, okay, so C++ is not as strongly typed as Java.

It's just that C++ supports in addition other programming paradigms aside from OOP: procedural and generic. C++ is curiously torn between keeping a language that is "close to the machine" (ergo pointers, free subprograms, implementation-dependent primitives, etc) while at the same time sustaining an OO approach. Such a situation does not appear to please the OO purists among C++ programmers too.
-anthony
 
subhashis sarangi
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi i think , in the concept of inheritance every class always refer to its super class and as Object class is the super class of every class so there is not multiple inheritance in java.
 
Guy Allard
Ranch Hand
Posts: 776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Greg Brouelette:
Interestingly enough, Bjarne Stroustrup, then author of the C++ language, has said that if he had to do it all over again, he would have left multiple inheiritence out of C++.


I am curious when and where he said that.

At the time MI was being discussed for inclusion in C++ there were raging discussions as to good/bod/indifferent.

I think I recall Stroustrup saying one of the reasons MI was included is that it was 'very simple to implement'.

One of the other guys on the design/implementation team said (roughly): MI is like a parachute - you do not need one very often, but when you do are very glad to have it.

Guy
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Guy Allard:

I think I recall Stroustrup saying one of the reasons MI was included is that it was 'very simple to implement'.


I'm sure he never said that, and if he did, I'm sure he'd be horribly embarrassed by having said it. It took many, many years for C++ compiler implementors to figure out how to do MI correctly, and for the C++ standard to be revised suitably to remove enough ambiguities and contradictions in relation to MI to make it possible to use the feature in a reasonably portable way. It's a devilishly hard thing to implement correctly, and it was left out of Java deliberately to avoid the pain and sorrows it brought to the C++ community.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Guy may be thinking of this, from Stroustrup's The Design and Evolution of C++, 1994, p 270:
I implemented multiple inheritance in a way that imposed and overhead even if the user didn't use anything but single inheritance. This violated the "you don't pay for what you don't use" rule (4.5) and led to the (false) impression that multiple inheritance is inherently inefficient. I considered the overhead acceptable because it was small (one array access plus one addition per virtual function call), and because I knew a simple technique for implementing multiple inheritance so that there is absolutely no change in the implementation of a virtual function call in a single inheritance hierarchy (12.4). I chose the "sub-optimal" implementation because it was more portable.

Make of that what you will...
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15216
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To come back to the original question: Multiple inheritance is not a feature that you really need and it is the cause of a number of issues like the diamond problem.

Adding multiple inheritance (with multiple base classes) to Java probably causes more problems than that it solves.

The designers of the Java language wanted to avoid those issues that make C++ so complicated, and this is one of them.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic