This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Java in General and the fly likes How  can multiple inheritance solve the problem of Deadly Diamond of Death Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "How  can multiple inheritance solve the problem of Deadly Diamond of Death" Watch "How  can multiple inheritance solve the problem of Deadly Diamond of Death" New topic
Author

How can multiple inheritance solve the problem of Deadly Diamond of Death

ragi singh
Ranch Hand

Joined: Mar 10, 2010
Posts: 198

Hi

Java does not support multiple inheritance , because of the Deadly Diamond of Death problem . however the problem can be solved by multiple inheritance in Interfaces . Can anyone explain ?
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

The diamond problem can occur using interfaces. An example:



"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Hauke Ingmar Schmidt
Rancher

Joined: Nov 18, 2008
Posts: 433
    
    2
Java allows multiple inheritance with interfaces but not with implementations.

This helps if two interfaces define the same method signature. It doesn't matter which interface defined the method if a class implements both. So there is no problem in resolving which code to call as it would be when two super classes would provide method implementations for methods with the same signature.

It doesn't help when methods in different interfaces are not compatible, i.e. have different return types or have generified types that lead to the same method signature after type erasure.

Oh, and obviously it doesn't allow you to pull in code from different base implementations.
Ram Narayan.M
Ranch Hand

Joined: Jul 11, 2010
Posts: 247

Why Multiple Inheritance is not supported in Java Classes?...

What i think is...

Multiple Inheritance is nothing but different characteristics of two or more parent classes is inherited by Children classes. Same characteristic from two or more parent class would take into Children class to dilemma of what class to select to inherit that characteristic.

Since all Java classes is designed in such a way of extending "Object" class... So, Multiple Inheritance property falls short due to this design... So Multiple Inheritance property is brought into Java in form of Interfaces...


SCJP 6 [SCJP - Old is Gold]
ragi singh
Ranch Hand

Joined: Mar 10, 2010
Posts: 198

Thanks Hauke i can understand it now completely.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38463
    
  23
No, Java does not allow multiple inheritance at all. Using Object does not constitute multiple inheritance, only a longer inheritance "tree". An interface extending several other interfaces does not constitute multiple inheritance.

Implementing multiple interfaces does not constitute multiple inheritance either, but it allows one to mimic it.
Hauke Ingmar Schmidt
Rancher

Joined: Nov 18, 2008
Posts: 433
    
    2
The JLS itself speaks of multiple inheritance for interfaces: JLS - 9. Interfaces.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38463
    
  23
Hauke Ingmar Schmidt wrote:The JLS itself speaks of multiple inheritance for interfaces: JLS - 9. Interfaces.
If you look at the most recent edition of the JLS, however, and use ctrl-F “multiple inheritance”, the phrase only comes up once as something prohibited.
Hauke Ingmar Schmidt
Rancher

Joined: Nov 18, 2008
Posts: 433
    
    2
Campbell Ritchie wrote:
Hauke Ingmar Schmidt wrote:The JLS itself speaks of multiple inheritance for interfaces: JLS - 9. Interfaces.
If you look at the most recent edition of the JLS, however, and use ctrl-F “multiple inheritance”, the phrase only comes up once as something prohibited.


Because they use brackets now: "This (multiple) interface inheritance allows objects to support (multiple) common behaviors without sharing any implementation." http://docs.oracle.com/javase/specs/jls/se7/html/jls-9.html
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How can multiple inheritance solve the problem of Deadly Diamond of Death