EDN Admin
Well-known member
Why? To avoid diamond problem.
Now, What is diamond problem?.....J
Let me explain….
Let’s consider a class ‘A’ which has sub classes ‘B’ and ‘C’. And there is a class ‘D’ which inherits both ‘B’ and ‘C’. Now, this is exactly a case of multiple inheritance [D inheriting B and C]. Now, as per OOPs ‘D’ can access public methods of ‘A’. Let’s consider there is a Method named TEST() in ‘A’ which is differently overridden by ‘B’ and ‘C’ classes. Now, if we access the method TEST() through the object ‘D’, which behavior of TEST() will get invoked?
B’s implementation or C’s??
This problem is exactly termed asDiamond Problem.
Ok …. Again, I have another scenario. I have some classes named Tiger, Animal and Mammal. Now, I can expect my Tiger class to get inherited from both the classes Animal and Mammal. How this can be achieved without Multiple inheritance?
Of course! This is achieved through Multiple Interface Inheritance. [C# won’t support Multiple Implementation Inheritance, but on the other hand it supports Multiple Interface Inheritance]
Now, wont we encounter Diamond Problem in Interface inheritance? No! Because, the problem will be there, on the different implementation of a same method by different classes. But here, we are inheriting only the definition of the methods and not its implementations. So, there is no question of creating diamond problem.J
View the full article
Now, What is diamond problem?.....J
Let me explain….
Let’s consider a class ‘A’ which has sub classes ‘B’ and ‘C’. And there is a class ‘D’ which inherits both ‘B’ and ‘C’. Now, this is exactly a case of multiple inheritance [D inheriting B and C]. Now, as per OOPs ‘D’ can access public methods of ‘A’. Let’s consider there is a Method named TEST() in ‘A’ which is differently overridden by ‘B’ and ‘C’ classes. Now, if we access the method TEST() through the object ‘D’, which behavior of TEST() will get invoked?
B’s implementation or C’s??
This problem is exactly termed asDiamond Problem.
Ok …. Again, I have another scenario. I have some classes named Tiger, Animal and Mammal. Now, I can expect my Tiger class to get inherited from both the classes Animal and Mammal. How this can be achieved without Multiple inheritance?
Of course! This is achieved through Multiple Interface Inheritance. [C# won’t support Multiple Implementation Inheritance, but on the other hand it supports Multiple Interface Inheritance]
Now, wont we encounter Diamond Problem in Interface inheritance? No! Because, the problem will be there, on the different implementation of a same method by different classes. But here, we are inheriting only the definition of the methods and not its implementations. So, there is no question of creating diamond problem.J
View the full article