Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
ragi singh
Ranch Hand
Posts: 198
Oracle Suse
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The diamond problem can occur using interfaces. An example:

 
Hauke Ingmar Schmidt
Rancher
Posts: 436
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 247
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
ragi singh
Ranch Hand
Posts: 198
Oracle Suse
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Hauke i can understand it now completely.
 
Campbell Ritchie
Sheriff
Pie
Posts: 48984
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 436
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The JLS itself speaks of multiple inheritance for interfaces: JLS - 9. Interfaces.
 
Campbell Ritchie
Sheriff
Pie
Posts: 48984
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 436
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic