As we know, Object oriented programming helps us to reuse common
Data Members and Methods in different class with the help of Object Oriented
Programming Concept called Inheritance.
In general, there are five types of inheritance –
Among these, Multiple Inheritance is not supported in .Net.
Above diagram represents Multiple Inheritance example which is not supported in .Net.
There is a possibility that Multiple Inheritance will cause ambiguity
problems. It happens when two or more parent classes will have members with same
identifier. Let’s understand this by an example.
So basically, Multiple Inheritance is not supported in .Net to avoid ambiguity. However, Interface is an alternate that can be used in .Net to avail Multiple Inheritance.
In general, there are five types of inheritance –
- Single Inheritance
- Multiple Inheritance
- Hierarchical Inheritance
- Multilevel Inheritance
- Hybrid Inheritance
Among these, Multiple Inheritance is not supported in .Net.
What is Multiple Inheritance?
When a class acquires features from two or more base
classes, it is called Multiple Inheritance.
Above diagram represents Multiple Inheritance example which is not supported in .Net.
Let’s see now the reasons for this diamond problem – Why
multiple inheritance is not supported in C#/VB.Net?
In above example –
- Class A and Class B have same method called “Calculate(int val1, int val2)”.
- Class C inherits Class A & Class B. [.Net does not support this. It is shown here for example only].
- In Class C, we are trying to call a method from base class - “base.Calculate(10, 10)”.
- Now here, ambiguous situation arises. Compiler cannot identify which method to call, from Class A OR Class B.
- To avoid this situation, Multiple Inheritance is not supported in .Net.
So basically, Multiple Inheritance is not supported in .Net to avoid ambiguity. However, Interface is an alternate that can be used in .Net to avail Multiple Inheritance.
No comments:
Post a Comment