This week's book giveaway is in the Design forum.
We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!
See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why Java does not multiple inheritance

 
Gaurav Ram
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • 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
 
Kevin Waddell
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • 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?
 
Robert Hill
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
  • 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.
 
Nicholas Jordan
Ranch Hand
Posts: 1282
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
  • Quote
  • Report post to moderator
Sir thanks for your reply but i am still confused can you please elaborate it little bit more.
 
David McCombs
Ranch Hand
Posts: 212
  • Mark post as helpful
  • send pies
  • 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.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15214
36
Android IntelliJ IDE Java Scala Spring
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic