Let me tell you a scenario.
I have a class Shape1
It has CalcualteArea method
Class Shape1
{
public void CalculateArea()
{
//
}
}
There is another class shape2 that one also has same method
Class Shape2
{
public void CalculateArea()
{
//
}
}
Now i have a child class Circle, it derives from both SHape1 and shape2;
public class Circle: Shape1,shape2
{
}
Now when i create object for Circle, and call the method, system doesn't know which calculate area method to be called.. Both has same signatures. So compiler will get confuse .that's why multiple inheritances are not allowed.
But there can be multiple interfaces because interfaces dont ve methjod definition..Even both the interfaces have same method, both of them dont ve any implementation and always method in the child class will be executed..
------------------------------------------------------
To understand that you need to understand the Diamond problem first:
The Diamond problem is an ambiguity that arises when two classes B and C inherit from Class A and class D inherits from both B and C. If a method in D calls a method defined in A(and does not override the method), and B and C have overridden that method differently, then from which class does it inherit: B or C?
That is the reason why C# and Java languages does not support multiple inheritance.
The Diamond problem is an ambiguity that arises when two classes B and C inherit from Class A and class D inherits from both B and C. If a method in D calls a method defined in A(and does not override the method), and B and C have overridden that method differently, then from which class does it inherit: B or C?
That is the reason why C# and Java languages does not support multiple inheritance.
A diamond class inheritance diagram.
The "diamond problem" (sometimes referred to as the "deadly diamond of death"[5])
is an ambiguity that arises when two classes B and C inherit from A,
and class D inherits from both B and C. If there is a method in A that B
and/or C has overridden, and D does not override it, then which version of the method does D inherit: that of B, or that of C?
For example, in the context of GUI software development, a class
Button may inherit from both classes Rectangle (for appearance) and Clickable (for functionality/input handling), and classes Rectangle and Clickable both inherit from the Object class. Now if the equals method is called for a Button object and there is no such method in the Button class but there is an overridden equals method in Rectangle or Clickable (or both), which method should be eventually called?
It is called the "diamond problem" because of the shape of the class
inheritance diagram in this situation. In this case, class A is at the
top, both B and C separately beneath it, and D joins the two together at
the bottom to form a diamond shape
http://en.wikipedia.org/wiki/Diamond_problem#The_diamond_problem
http://www.artima.com/intv/dotnetP.html
http://blogs.msdn.com/b/csharpfaq/archive/2004/03/07/85562.aspx
No comments:
Post a Comment