aspose file tools*
The moose likes Beginning Java and the fly likes Why Java does not multiple inheritance Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Why Java does not multiple inheritance" Watch "Why Java does not multiple inheritance" New topic
Author

Why Java does not multiple inheritance

Gaurav Ram
Ranch Hand

Joined: Feb 23, 2008
Posts: 32
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

Joined: Mar 07, 2008
Posts: 3
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

Joined: Feb 24, 2006
Posts: 94
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

Joined: Sep 17, 2006
Posts: 1282
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 ]

"The differential equations that describe dynamic interactions of power generators are similar to that of the gravitational interplay among celestial bodies, which is chaotic in nature."
Gaurav Ram
Ranch Hand

Joined: Feb 23, 2008
Posts: 32
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

Joined: Sep 17, 2006
Posts: 1282
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

Joined: Feb 23, 2008
Posts: 32
Sir thanks for your reply but i am still confused can you please elaborate it little bit more.
David McCombs
Ranch Hand

Joined: Oct 17, 2006
Posts: 212
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.


"Should array indices start at 0 or 1? My compromise of 0.5 was rejected without, I thought, proper consideration."- Stan Kelly-Bootle
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14352
    
  22

Have a look at this: Diamond problem


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why Java does not multiple inheritance