This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Java in General and the fly likes multiple inheritance Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "multiple inheritance" Watch "multiple inheritance" New topic
Author

multiple inheritance

yashendra chandrakar
Greenhorn

Joined: Mar 22, 2002
Posts: 22
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

Joined: Mar 22, 2002
Posts: 1055
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

Joined: Nov 14, 2000
Posts: 1953
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

Joined: Jan 23, 2002
Posts: 144
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++.


For a good Prime, call:<br />29819592777931214269172453467810429868925511217482600306406141434158089
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
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...


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Anthony Villanueva
Ranch Hand

Joined: Mar 22, 2002
Posts: 1055
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

Joined: Nov 03, 2006
Posts: 3
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

Joined: Nov 24, 2000
Posts: 776
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

Joined: Jul 08, 2003
Posts: 24183
    
  34

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.


[Jess in Action][AskingGoodQuestions]
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
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...


"I'm not back." - Bill Harding, Twister
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

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.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: multiple inheritance
 
Similar Threads
Object class by default break to rule of multipul inheritance
Java Supports Multiple Inheritance without interfaces
Why no mutiple inheritance
Multiple inheritance (was: Raghavendra)
How can multiple inheritance solve the problem of Deadly Diamond of Death