AllExperts > Java 
Search      
Java
Volunteer
Answers to thousands of questions
 Home · More Java Questions · Answer Library  · Encyclopedia ·
More Java Answers
Question Library

Ask a question about Java
Volunteer
Experts of the Month
Expert Login

Awards

About Us
Tell friends
Link to Us
Disclaimer

 
 
 
 
About Beenish
Expertise
I can answer questions related to core java, jsp and servlets at an intermediate level.Jdbc, design patterns. Currently in process to be an SCJP and SCWCD certified developer. Please no questions regarding swing.

Experience
I have got three years experience working in Java.

Education/Credentials
Bachelors in Computer Sciences

 
   

You are here:  Experts > Computing/Technology > Focus on Java > Java > deadly diamond of death pblm

Java - deadly diamond of death pblm


Expert: Beenish - 11/3/2009

Question
hello
I am learning java nw.I saw that java doesn't allow multiple inheritance because of this pblm. As a solution they are providing interfaces where the functions are abstract ( so who ever is implementing must write the function).
I still don't get how they have got rid of the deadly diamond of death pblm? It would be nice if you can explain with an example. 'A' is the super class 'B' and 'C' inherit from 'A' , then 'D' is inheriting from 'B' and 'C'. So this causes daimond pblm.So how you solve this using interface?

Answer
Hello,

Thankyou for your question.

Well, first you should completely understand what is the deadly diamond of death.
Lets say, if Java would have supported multiple inheritance which means inheriting
from one or more class. Lets say we had a class A which had some method doStuff().
Then lets say there were two classes B and C which extended from A. Both will inherit
method doStuff() and they both will provide their own implementation means overriding
the functionality provided by the super class. Now lets say if Java would have supported
multiple inheritance and another class D would have come into picture and it extended
from both class B and C, now at this point where the deadly diamond of death will occur.
Class D will be confused which implementation should i really inherit. Class B or C?

This kind of situation gets avoided by interfaces because interfaces are only contracts,
and they don't provide you with implementations. It is upto the classes to provide the
implementations which implement them. So such kind of version conflict will never occur
in case of interfaces as there are no implementation details provided just the signature.
No two implementations can ever conflict as in case of situation i described above.

Now lets say, if you have the following scenario

interface B{
void eat();
}

interface A{
void eat();
}

public class C implements B,A{

  public void eat() {
     // TODO Auto-generated method stub
     
  }

}

In this case only one eat() method will exist in class C because duplicate methods
are not allowed. Does this make sense to you now?

Hope this helps,
Ben

Add to this Answer   Ask a Question


 
User Agreement | Privacy Policy | Kids' Privacy Policy | Help
Copyright  © 2008 About, Inc. AllExperts, AllExperts.com, and About.com are registered trademarks of About, Inc. All rights reserved.