• 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:
  • Devaka Cooray
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Saloon Keepers:
  • Tim Holloway
Bartenders:

Why Java does not multiple inheritance

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

Can you please explain me that what exactly is Deadly Diamond of Death problem related to multiple inheritance due to which java does not supports Multiple Inheritance.

Thanks,
Gaurav
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If Java allowed Multiple Inheritance, then imagine this:

Class Game has an instance variable: PlayerName

Class MyGame inherits Game (including its variable PlayerName - set this to my name).
Class YourGame also inherits Game (including its variable PlayerName - set this to your name).

Now if we were able to inherit both MyGame and YourGame together in another class called BothGames, and we wanted to access both of our names, how would we do it? There is only one variable called PlayerName.

The same could also be done with methods with the same name instead of instance variables. If both MyGame and YourGame classes override a method in the Game class and BothGames inherits both MyGame and YourGame, which method would it use?
 
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
They could have allowed it. The ambiguity can be easily solved. It isn't a massive issue in C++ with a little care.

They didn't allow it and came up with interfaces which is slightly more flexible.
 
Ranch Hand
Posts: 1282
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here are the words of someone who actually works in the Deadly Diamond of Death: ...(you need to use the ParameterSpec **** instead) (cleaned up for beginner area) which show the nature of the problem. Some classificaton issues do not resolve cleanly into a first, second, third ordering - when you write code, you will discover the difficulty when trying to orgainze your program logic.
[ March 08, 2008: Message edited by: Nicholas Jordan ]
 
Gaurav Ram
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks all for replying. Like Kevin said:

Now if we were able to inherit both MyGame and YourGame together in another class called BothGames, and we wanted to access both of our names, how would we do it? There is only one variable called PlayerName.



We can do one thing that we can make reference of particular class and access its method/instance variables, like if we want to access instance variable PlayerName, we can write.... and same for other class also.

Will this solution does not work?
 
Nicholas Jordan
Ranch Hand
Posts: 1282
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay, there are a lot of people here who can fathom this base class / super class stuff really well but for now, what you are asking is this;

One common trait, intrinsic or property - the are both a game. So write one class and in that class call it abstract class. Then have both myGame / yourGame extend game. Then to get my player, you do it like this:



Here, both players are GamePlayers, which have class Game as common base class, but you will notice I have made no way to give them different names. This discussion is easy to get ahead of yourself, but can be learned if you use a ladder approach to build the ideas.
[ March 08, 2008: Message edited by: Nicholas Jordan ]
 
Gaurav Ram
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sir thanks for your reply but i am still confused can you please elaborate it little bit more.
 
Ranch Hand
Posts: 212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Gaurav Ram:
Thanks all for replying. Like Kevin said:


We can do one thing that we can make reference of particular class and access its method/instance variables, like if we want to access instance variable PlayerName, we can write.... and same for other class also.

Will this solution does not work?




That would work because you have two references to two different objects. There is no ambiguity and no inheritance involved in doing so.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have a look at this: Diamond problem
 
Bring me the box labeled "thinking cap" ... and then read this tiny ad:
The new gardening playing cards kickstarter is now live!
https://www.kickstarter.com/projects/paulwheaton/garden-cards
reply
    Bookmark Topic Watch Topic
  • New Topic