• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Multiple inheritance.

 
Ranch Hand
Posts: 154
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Ranchers,

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.
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"Modeling the real world" in the sense you seem to be using it often leads to quite inferior, if not totally inadequate, designs.

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
 
Rakesh Jhamb
Ranch Hand
Posts: 154
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the wonderful explanation, and I completely agree.

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
 
Ranch Hand
Posts: 1936
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One of multiple inheritance problems is it can lead to confusion, if two or more parent classes have the same attributes/methods, which ones will be inherited?

In your case, we can model like this:

Only one Entity (Person) is necessary.
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
IMO that makes it difficult to easily identify features that are inherited from both mother and father, although obviously that's a matter of a simple utility method. To me it seems like features exist as an isolated trait, and *may* be inherited from the mother, the father, or nobody: maybe it'd make more sense to have a map of features, with a possible list of who they're inherited from.
 
Hong Anderson
Ranch Hand
Posts: 1936
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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]
 
Ranch Hand
Posts: 2187
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Java does support multiple inheritance.

It "does not" support multiple inheritance of concrete classes.

It "does" support multiple inheritence of interface classes.






or







 
Hong Anderson
Ranch Hand
Posts: 1936
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Jimmy Clark
Ranch Hand
Posts: 2187
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A Java-based "interface" class is a type of "class." A Java-based "concrete" class is also a type of "class."

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.
 
Ranch Hand
Posts: 686
Netbeans IDE Chrome Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Good discussion. I would like to follow this. I am still not sure what I should be the correct answer. Interfaces are like contracts without the implementation details. Not sure if they would classify as inheritance
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic