Java Forum / General / March 2006
Multiple Inheritance
freesoft_2000 - 19 Mar 2006 13:40 GMT Hi everyone,
Is multiple inheritance allowed in java??
If yes how does one go about doing it. A simple example would be helpful
Richard West
opalpa@gmail.com opalinski from opalpaweb - 19 Mar 2006 13:50 GMT > Is multiple inheritance allowed in java?? No, it is not allowed.
Opalinski opalpa@gmail.com http://www.geocities.com/opalpaweb/
Cruella DeVille - 19 Mar 2006 13:56 GMT The answere is both yes and no. You can't have a subclass inheriting from multiple superclasses, but you could implement an interface with the desired properties.
Chris Uppal - 19 Mar 2006 14:06 GMT > Is multiple inheritance allowed in java?? Depends on whether you mean inheritance of implementation, or inheritance of interface. The answers are no and yes respectively.
-- chris
IchBin - 19 Mar 2006 14:58 GMT > Hi everyone, > [quoted text clipped - 4 lines] > > Richard West No, you can not define multiple inheritance but. You can use multiple Interfaces.
"The reasons for omitting multiple inheritance from the Java language mostly stem from the "simple, object oriented, and familiar" goal. As a simple language, Java's creators wanted a language that most developers could grasp without extensive training. To that end, they worked to make the language as similar to C++ as possible (familiar) without carrying over C++'s unnecessary complexity (simple).
In the designers' opinion, multiple inheritance causes more problems and confusion than it solves. So they cut multiple inheritance from the language (just as they cut operator overloading). The designers' extensive C++ experience taught them that multiple inheritance just wasn't worth the headache.
Note: For a discussion of the diamond problem, a classic multiple inheritance challenge, read Bill Venners's "Designing with Interfaces" (JavaWorld, December 1998) and Tony Sintes's "Java Diamonds Are Forever" (JavaWorld, March 2001).
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."
http://www.javaworld.com/javaqa/2002-07/02-qa-0719-multinheritance.html
Thanks in Advance... IchBin, Pocono Lake, Pa, USA http://weconsultants.servebeer.com/JHackerAppManager __________________________________________________________________________
'If there is one, Knowledge is the "Fountain of Youth"' -William E. Taylor, Regular Guy (1952-)
opalpa@gmail.com opalinski from opalpaweb - 19 Mar 2006 17:05 GMT > Multiple interface inheritance still allows an object to inherit > methods and to behave polymorphically on those methods. Well, as far as subclassing from one class we get to inherit methods from it and its superclasses, but in Java we do not get to inherit methods from mutliple classes along different paths towards Object. We get to inherit method signatures from multiple parent paths, not methods.
> The inheriting > object just doesn't get an implementation free ride." "just"? "free ride"?
I believe Gosling made correct decision in not permitting multiple inheritence, but the last paragraph in the exceprt from javaworld is poorly written (it's not multiple interface inheritance that allows an object to inhert methods and behave polymorphically, single inhertience does that) and a little strange ("just" "free ride").
Opalinski opalpa@gmail.com http://www.geocities.com/opalpaweb/
neuneudr@yahoo.fr - 19 Mar 2006 19:16 GMT > > Multiple interface inheritance still allows an object to inherit > > methods and to behave polymorphically on those methods. [quoted text clipped - 15 lines] > object to inhert methods and behave polymorphically, single inhertience > does that) and a little strange ("just" "free ride"). I guess you'll find the following sentences too poorly written too:
"Java developers all too often associate the term polymorphism with "an object's ability to magically execute correct method behavior at "appropriate points in a program. That behavior is usually associated "with overriding inherited class method implementations. However, a "careful examination of polymorphism demystifies the magic and reveals "that polymorphic behavior is best understood in terms of type, rather "than as dependent on overriding implementation inheritance. That "understanding allows developers to fully take advantage of polymorphism.
"...that implementation-oriented view leads to images of wizardry, "rather than an understanding of fundamental concepts.
"Polymorphism in Java is invariably subtype polymorphism. "... "A fundamental understanding of subtype polymorphism requires that "you make the shift from implementation concerns to thinking in terms "of type.
These sentences comes from the article linked in the article you quoted:
http://www.javaworld.com/javaworld/jw-04-2001/jw-0413-polymorph_p.html
To me it's all about ADT and I don't see anything wrong with the sentence that you find poorly worded:
"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.
It's exactly like that and there are many here on c.l.j.p. who could show you polymorphism at work on an object inheriting from several interfaces.
You wrote:
> (it's not multiple interface inheritance that allows an object > to inhert methods and behave polymorphically, single > inhertience does that) It's not that I find your "single inhertience [sic] does that" wrong, I simply thinks it doesn't make any sense.
I think that polymorphism is not what *you* think.
Bye
opalpa@gmail.com opalinski from opalpaweb - 19 Mar 2006 21:34 GMT It appears that we are using two definitions of method. I was under the impression that in order to be a method something would need a body -- executable code. I would not call members of an interface methods but instead method signatures. I looked into Java spec on Sun's site and see that the spec does not agree with my definition of method. The spec has a grammar rule:
MethodBody: Block ;
That is a method does not need to contain executable code to be a method, a single ';' is considered a complete method body. I did not know that was the definition of method in Java language.
Opalinski opalpa@gmail.com http://www.geocities.com/opalpaweb/
Chris Uppal - 20 Mar 2006 14:31 GMT opalpa@gmail.com wrote:
> I looked into Java spec on Sun's site > and see that the spec does not agree with my definition of method. The > spec has a grammar rule: I wouldn't read too much into the words used in the grammar spec. When you write a formal grammar you very quickly run out of descriptive words and have to start hacking around with approximations. Also you want to be able to reuse grammar rules, so you can end up using the right word for a concept in one place, and then reusing the rule in another place where the word itself is incorrect.
-- chris
Roedy Green - 19 Mar 2006 19:02 GMT On Sun, 19 Mar 2006 07:40:16 -0500, "freesoft_2000" <freesoft_2000@yahoo.com> wrote, quoted or indirectly quoted someone who said :
> Is multiple inheritance allowed in java?? the closest thing you have is implementing multiple interfaces.
See http://mindprod.com/jgloss/interface.html
 Signature Canadian Mind Products, Roedy Green. http://mindprod.com Java custom programming, consulting and coaching.
Timo Stamm - 19 Mar 2006 21:27 GMT Roedy Green schrieb:
> On Sun, 19 Mar 2006 07:40:16 -0500, "freesoft_2000" > <freesoft_2000@yahoo.com> wrote, quoted or indirectly quoted someone [quoted text clipped - 3 lines] > > the closest thing you have is implementing multiple interfaces. You can get even closer by using java.lang.Proxy which can can compose the implementations.
The syntax is very bloated - you have to type a lot of instructions that are hidden by a language with support for multiple inheritance. But it's better than implementing all necessary delegators to the implementations by hand.
Timo
Tony Morris - 19 Mar 2006 23:25 GMT > Hi everyone, > [quoted text clipped - 4 lines] > > Richard West Java has multiple inheritance. It is called 'multiple contract inheritance'. You might run into a common misunderstanding that 'inheritance' applies only to behaviour inheritance - and the subsequent conclusion that Java does not support it. This is due to the original ideas from a language called Smalltalk and then being propagated along by marketers and/or Java language designers (who are distinct from those who research and understand language theory, etc.). The sheep will follow...
Java supports 'static contract inheritance', which means you can have a composite of contract through inheritance, however, the created type exists statically for the remainder of its existence. This is an unfortunate flaw with many adverse consequences (though they are often attributed elsewhere ala marketing again). Simply, Java (and all known type-safe languages) does not acknowledge the notion of 'computational progression' or even more simply that 'time moves' - Special Theory of Relativity aside. This - and other 'nasties' - has seen a recent trend towards dynamic typing, since you are permitted to meet your requirement at any given point in time, however, you are also forced to exceed it. A language that permits an alignment accurately with requirement - specifically, the axioms of software (see below) does not exist (publicly) that I am aware of.
This conversation quickly digresses to what I refer to as "The Axioms of Software", where one must state the definition of software, and then one can subsequently provide many logical reasonings and some very interesting conclusions using for example, some (or many) mathematical method of induction. For example, under the aforementioned axioms, one can disprove the legitimacy of the OO paradigm (as we know it, since it has no formal definition). It's really quite a fun thing to do, however, it attracts the critics who faithfully subscribe to what they have been taught - even if by themselves. In contrast, sound criticism permits you to digress into further insights that you might have previously been unaware of.
 Signature Tony Morris http://tmorris.net/
s/Commonwealth Games/Commonwealth Swimming
opalpa@gmail.com opalinski from opalpaweb - 20 Mar 2006 01:50 GMT "Java has multiple inheritance. "
James Gosling (creator of Java) writes: "Multiple inheritance--and all the problems it generates--was discarded from Java. " (from http://java.sun.com/docs/white/langenv/Simple.doc2.html#4090 atrributed to Gosling in table of contents http://java.sun.com/docs/white/langenv/ )
" You might run into a common misunderstanding that 'inheritance' applies only to behaviour inheritance - and the subsequent conclusion that Java does not support it. ... "
Bjarne Stroustrup (creator of C++) writes: "If common data structures or operations are needed for implementations they can be added in a base class that is visible only to implementers:
class common { // seen by implementers // data // functions };
class my_implementation : public interface, protected common { // data // functions // overriding functions };
class your_implementation : public interface, protected common { // data // functions // overriding functions };
This is one of the simplest and most fundamental uses of multiple inheritance." (from http://www.gotw.ca/publications/c_family_interview.htm)
> The sheep will follow... The men who created the languages are the sheep? I don't think so.
OK, Tony, you do state that these ideas are "being propagated along by marketers and/or Java language designers (who are distinct from those who research and understand language theory, etc.)."
So my noting Gosling and Stroustrup does not impress you. Fine. They are not just language designers, however, they are also implementors. They consider the reality of who programmers are right now and they consider the realites of organizations that utilize programs. You're touting the opinion of leisurely academics who do not consider such things. Idealism and leisurely contemplation are fun, however some take on the challange of reality -- making things work and progressing in the more immediate term. I'm saying I respect your position, and it is a welcome position for a discussion forum. I'm touting the definitions and opinions of those who assist me in day to day practice.
Whose opinions do you like? Robert Harper (http://www.cs.cmu.edu/~rwh/)? Are you a LISP programmer? An ML guy?
Anyway, my point is, Java, by common definition of multiple-inheritence, by the established practical definition of multiple-inheritence, does not have multiple-inheritence. So say the creators (designers and implementors) of Java and other languages, the millions of users, and most literature.
> IFor example, under the aforementioned axioms, one > can disprove the legitimacy of the OO paradigm (as we know it, since it > has no formal definition). The aforementioned axioms which you have nebulusly titled but failed to list...
OO has legitimacy from me as it helps me pay bills.
> In contrast, sound criticism permits you > to digress into further insights that you might have previously been > unaware of. Kudos, I respect that. Please continue. You've an audience, of at least one.
Good day mate,
Opalinski opalpa@gmail.com http://www.geocities.com/opalpaweb/
Tony Morris - 20 Mar 2006 02:32 GMT > "Java has multiple inheritance. " > [quoted text clipped - 86 lines] > opalpa@gmail.com > http://www.geocities.com/opalpaweb/ I think there is a misunderstanding - I am not touting the opinion of academics, and likewise, I will fervently defend the "realities of organizations" as legitimate within the context that we are discussing.
No, Stroustrup and Gosling do not impress me - we have moved on since then. Like many things, they set an excellent precedent (I am complementing Gosling here where it is most certainly unwarranted), being the best of the day, and I concede that Java still fits somewhere around "the best of the day". That it meets the "realities of organizations" is exactly the debate that I am engaged in. Simply, if you accept that the aforementioned (but not elaborated on as you picked up :)) Axioms of Software aligns with - as you put it - realities of organizations, then you also accept that Java (including the OO paradigm as we know it), and many other languages fall *far* short of meeting the requirement. In fact, I spend most of my development time conceding to, and working around, the many shortcomings as do others, only I observe them to attribute these workarounds to something else - sometimes even a "design pattern"!! How's that for marketing!?
Why is Java successful? Because it is a superior language? I can't help but view that answer as a mere subscription to the marketing material that I refer to. Externalise the perception (I admit, I once thought it as well!), place it under a heavy analysis (use whatever method you choose - they all derive the same conclusion), and then reconsider your position.
The crucial parts that I have omitted (and I acknowledge it) are the hitherto "axioms" and the logical steps to the conclusion. I'd be most interested in discussing the topic with interested parties, but engaging on public internet forums has proven fruitless in the past, so please accept my cowardly decline here.
Java has multiple inheritance until I see a sound proof to the contrary
:) What the dieties say means nothing - not even asymptote zero. In fact, what is often touted by the Gods can often be used to shortcut conclusions to hypotheses by method of contradiction. Simply assume what is said is false, then try to prove it true. By doing so, you often inadvertantly prove the contrary. This requires much externalisation of what many (at least i have found) accept faithfully as truth. I also used this at my previous employer (a large corporation) who also engaged in portraying illegitimate forms of authority as a premise of truth. Suffice to say, there are related reasons that I left :)
 Signature Tony Morris http://tmorris.net/
s/Commonwealth Games/Commonwealth Swimming
opalpa@gmail.com opalinski from opalpaweb - 20 Mar 2006 14:54 GMT What name do you propose for the expression which is in C++ and is not in Java? The following expression:
"If common data structures or operations are needed for implementations they can be added in a base class that is visible only to implementers:
class common { // seen by implementers // data // functions };
class my_implementation : public interface, protected common { // data // functions // overriding functions };
class your_implementation : public interface, protected common { // data // functions // overriding functions };
This is one of the simplest and most fundamental uses of multiple inheritance."
Are you saying we now need to call it "multiple implementation inheritance"?
Funny timeline: Ten years ago: Java does not have "multiple inhertiance". Now, without any language facilities being added to support it, through the stretching of "multiple inhertance" concept, Java has multiple-inhertiance . This changing is fine, words change meaning, I don't have a problem with that. I do want to know what new name you propose for the expression that is not part of Java but is part of C++.
Opalinski opalpa@gmail.com http://www.geocities.com/opalpaweb/
neuneudr@yahoo.fr - 20 Mar 2006 04:02 GMT ...
> James Gosling (creator of Java) writes: > "Multiple inheritance--and all the problems it generates--was discarded > from Java. " > (from http://java.sun.com/docs/white/langenv/Simple.doc2.html#4090 > atrributed to Gosling in table of contents > http://java.sun.com/docs/white/langenv/ ) To be honest, this appear under a HUGE heading that clearly states "Features removed from C and C++"... And the table of contents shows two authors, not one, so quote what you will if it fits your view of the world.
Then, to me, it can be read "Multiple inheritance, as done in C++, was discarded from Java". (remember that it's under a section called "Features removed from C and C++" before jumping guns)
> Bjarne Stroustrup (creator of C++) writes: ...
> This is one of the simplest and most fundamental uses of multiple > inheritance." In your implementation-based view of OO...
Moreover this quoting of Bjarne Stroustrup does certainly not imply that multiple interface inheritance is not multiple inheritance.
> So my noting Gosling and Stroustrup does not impress you. Fine. They > are not just language designers, however, they are also implementors. > They consider the reality of who programmers are right now and they > consider the realites of organizations that utilize programs. "appeal to number". A common logical fallacy. What has it to do with multiple inheritance?
> Anyway, my point is, Java, by common definition of > multiple-inheritence, by the established practical definition > of multiple-inheritence, does not have multiple-inheritence. that is blatantly wrong but if it makes you confortable to think that... I'll leave you the last word (I'll finish this post, but I won't be participating in this thread anymore).
> So say the creators (designers and implementors) of Java and > other languages, the millions of users, and most literature. "appeal to authority" and "appeal to number" once again. I've showed that your quoting of "Gosling" (if he really did write that) was flakey at best, I haven't seen any tangible evidence from any language creator in what you quoted.
OK, I'll play your game now...
A paragraph here from Bill Veners:
"I presented interfaces primarily as a special kind of multiple "inheritance: multiple inheritance of interface (the object-oriented "concept) without multiple inheritance of implementation.
See? See? He's even suggesting that multiple inheritance done with interface is conceptually more OO than multiple inheritance inheritance of implementation.
How's that an heresy?
But wait, there's more, he's asking a question to James Gosling and, contrarly to you, James doesn't jump gun on him saying that multiple inheritance in Java doesn't exist:
"Bill Venners: In Java you included multiple inheritance of "interface, but left out multiple inheritance of implementation. "Were you trying to say anything to designers by making the "interface a separate construct? Were you trying to say anything "by leaving out multiple inheritance of implementation? How "did that come about? " "James Gosling: It listened to people from the C++ and "Objective-C camps, and I tried to sort out who had the most "happy experiences. The Objective-C notion of a pure interface "with no implementation seemed to have worked out really well "for people. It avoids a lot of the sticky issues such as "disambiguation that people get into in C++.
Impressive no!?, the very first sentence contains both "multiple inheritance of interface / multiple inheritance of implementation"... Don't you think multiple interface inheritance might be MI after all ?
Here's the link:
http://www.artima.com/intv/gosling13.html
I remind you that Gosling also said he regretted that he allowed to do concrete class subclassing and would have gone "pure interface" (his words) if he were to design Java again (oh if only he had done it... Java would be way less crappier than it is and, more importantly, way less abused).
Oh, and strangely Tony Morris is the author of a testing framework that has been written without a single occurence of the keyword abstract and with all classes declared final (no concrete subclassing). I haven't verified it, but I *know* this is 100% doable, as is multiple inheritance, in Java.
So now we're waiting for your proof that multiple inheritance isn't possible in Java. And don't forget to inform Bill Venners, James Goslings and all the language creators, for it's 2006 and multiple inheritance of "specification" (as objective-c calls it if I'm not mistaken) or multiple inheritance of interface is taken for granted.
And this should change: obviously the world must adapt to your definition of MI.
Some people in this thread answered "yes and no" to the original poster (and clearly explained that Java supported MI using interfaces but not MI using concrete inheritance). Me and Tony Morris both answered "yes" (and there's more than two of us as I hope you start to realize).
So far you're the only who answered and defended a "no".
Makes you wonder a tiny, little bit or not even?
opalpa@gmail.com opalinski from opalpaweb - 20 Mar 2006 14:38 GMT > Then, to me, it can be read "Multiple inheritance, as done in C++, > was discarded from Java". C++ practice includes implementing multiple interfaces as well as inherting implementations from multiple classes. So what new name do your propose, for the thing that used to be called "multiple inheritance", is still usually called "multiple inheritance", that I cannot do in Java, that Stroustrup is referring to when he typed:
"If common data structures or operations are needed for implementations they can be added in a base class that is visible only to implementers:
class common { // seen by implementers // data // functions };
class my_implementation : public interface, protected common { // data // functions // overriding functions };
class your_implementation : public interface, protected common { // data // functions // overriding functions };
This is one of the simplest and most fundamental uses of multiple inheritance."
----
> I remind you that Gosling also said he regretted that he allowed > to do concrete class subclassing and would have gone "pure > interface" (his words) if he were to design Java again (oh if > only he had done it... Remind me? It does not appear in the article you noted, nor does it appear in your previous post. Concrete class subclassing has its place in top-notch practice.
> And this should change: obviously the world must adapt to > your definition of MI. Err, you're the one pushing a radical definition. I'm the one trying to stick to the established definition.
----
What you call "appeal to number", I call "faith in free markets".
What you call "appeal to authority", I call "opinion of experts".
All the best, Opalinski opalpa@gmail.com http://www.geocities.com/opalpaweb/
Chris Uppal - 20 Mar 2006 14:46 GMT > Some people in this thread answered "yes and no" to the original > poster (and clearly explained that Java supported MI using [quoted text clipped - 3 lines] > > So far you're the only who answered and defended a "no". As one of the people who answered "yes and no", I'll defend a blank "no" answer.
There are two concepts of inheritance. Unfortunately we use the same name for both, but that doesn't mean they are particularly closely related. Interface inheritance is not "sort of like" implementation inheritance. Multiple inheritance of interface is not a sort of "cut-down" form of multiple inheritance of implementation.
So when someone asks, as the OP did, "Is multiple inheritance allowed in java??", you can either guess at which kind of inheritance he meant, or you can give a conditional answer. I would say that the overwhelming likelihood is that the OP (or anyone else who asks that question) is asking about implementation inheritance. Indeed, if they know enough to realise that there is such a thing as interface inheritance, then they'll almost certainly know enough to phrase the question differently (and can probably find the answer easily for themselves anyway).
Pursuing that thought, if the OP had asked "does Java have multiple inheritance of implementation", then of course the answer is a blanket "no". But that, I contend, is what the OP /did/ ask -- just in different, and less precise, words. That's what the OP /meant/. So to answer with a load of shilly-shallying about "what sort of inheritance do you mean?" is less than helpful. (And, yes, I was aware of that when I write my own answer -- I was feeling mean).
-- chris
Scott Ellsworth - 20 Mar 2006 21:30 GMT In article <ca0f6164326d0607c3ddf841ed975477@localhost.talkaboutprogramming.com>,
> Is multiple inheritance allowed in java?? What virtually everyone outside of Java and ObjectiveC mean by multiple inhertince is not allowed in Java. You cannot make an object that inherits the method implementations and instance data of two parent classes.
You can, though, inherit from as many interfaces as you like. Interfaces list methods that must be defined in implementing classes, but they do not contain any instance data.
Thus, you can usually get what you want from MI if you are after a behavior, but you cannot if you are after data.
public interface PlatonicSolid{ Boolean hasFourSides(); }
public interface ArchitecturalElement{ Boolean isUglyInLargeQuantities(); }
public class Tetrahedron{ Boolean hasFourSides(){ return true; } Boolean isUglyInLargeQuantities(){ return false; } }
So, a tetrahedron is both a platonic solid and an architectural element. I might have a better hierarchy if all architectural elements were Shapes, and PlatonicSolid was a subclass of Shape, but given the hierarchy as designed, this lets me have a class that is two different things.
I cannot, though, stuff instance data in either of those interfaces, so if you want a 'quality' int in ArchitecturalElement, you need that to be an actual class.
Scott
 Signature Scott Ellsworth scott@alodar.nospam.com Java and database consulting for the life sciences
Free MagazinesGet these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...
|
|
|