Multiple inheritance.
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
As java does not support multiple inheritance to avoid ambiguity, but then how do we simulate real world, for example in the following case....
Let us there is a class called Father and class called Mother.
Then there is a class called Child that should inherit attributes as well as behavior from both Father and Mother, how do we do it in Java ?
Thanks.
SCJP2, SCWCD
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Let's take your example - there are a whole load of problems with having a Child class inherit from Mother and Father classes:
- a Child can be - or at least become - a Mother or Father, too
- a Child doesn't inherit all attributes and operations from both Mother and Father - it inherits some from Mother, some from Father
- when we talk about inheriting attributes, in your real life example we mean attribute *values*, like eye-color: green. In class inheritance, we mean the *existance* of an attribute, such as "has an eye-color".
- object oriented programming is much more about behavior (operations) than attributes. And by that, I mean the behavior of objects in the software system, not the behavior of "objects" in the "real world".
So, before we can decide on a design, we need to know what the system is supposed to do, and what design forces we therefore experiences *in the code*.
For example, for some system, it might in fact make more sense to have Person class with two attributes "father" and "mother" that again point to Person instances.
See also http://www.artima.com/weblogs/viewpost.jsp?thread=37870
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
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
What I am still interested in knowing is that let us say we are in a situation where we are designing a medical system that keeps track of attributes / behaviors inherited from parents into child and clearly identify that it is inherited from father or mother....so there are 3 important entities father , mother and child.
Then in such a scenario what should be a good approach to start with our class design?
Thanks
SCJP2, SCWCD
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
In your case, we can model like this:
Only one Entity (Person) is necessary.
SCJA 1.0, SCJP 1.4, SCWCD 1.4, SCBCD 1.3, SCJP 5.0, SCEA 5, SCBCD 5; OCUP - Fundamental, Intermediate and Advanced; IBM Certified Solution Designer - OOAD, vUML 2; SpringSource Certified Spring Professional
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
David Newton wrote:IMO that makes it difficult to easily identify features that are inherited from both mother and father
No problem, just add getInheritedFeaturesFromBothMotherAndFather method.
Actually, I just want to point out that this requirement needn't multiple inheritance at all.
[Edited - changed method name]
SCJA 1.0, SCJP 1.4, SCWCD 1.4, SCBCD 1.3, SCJP 5.0, SCEA 5, SCBCD 5; OCUP - Fundamental, Intermediate and Advanced; IBM Certified Solution Designer - OOAD, vUML 2; SpringSource Certified Spring Professional
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
It "does not" support multiple inheritance of concrete classes.
It "does" support multiple inheritence of interface classes.
or
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
James Clark wrote:Java does support multiple inheritance.
No, it doesn't.
The Java programming language does not permit multiple inheritance (inheritance is discussed later in this lesson), but interfaces provide an alternative.
http://java.sun.com/docs/books/tutorial/java/IandI/createinterface.html
Multiple inheritance--and all the problems it generates--was discarded from Java. The desirable features of multiple inheritance are provided by interfaces--conceptually similar to Objective C protocols.
http://java.sun.com/docs/white/langenv/Simple.doc2.html
SCJA 1.0, SCJP 1.4, SCWCD 1.4, SCBCD 1.3, SCJP 5.0, SCEA 5, SCBCD 5; OCUP - Fundamental, Intermediate and Advanced; IBM Certified Solution Designer - OOAD, vUML 2; SpringSource Certified Spring Professional
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
The Java language supports multiple inheritance of interface classes, not concrete classes. A deep understanding of "what a class is", is required to accurately interpret the material on the web pages referenced above.
With only a cursory understanding, limited to web page content only, then it will be difficult know what it means when it is written, "Java does not support multiple inheritance."
Again, the Java language supports multiple inheritance of interface classes. The author of the material quoted above chose not to include the adjective "concrete." That is all.
Java's alternative to mulitple concrete class inheritance is mulitple interface class inheritance.
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
===Vyas Sanzgiri===
My Blog
| He loves you so much! And I'm baking the cake! I'm going to put this tiny ad in the cake: The new gardening playing cards kickstarter is now live! https://www.kickstarter.com/projects/paulwheaton/garden-cards |











