In every .net interview there is a common question is "why multiple inheritances is not allowed in C#?"
So, the answer is when we try to implement multiple inheritances using multiple base classes diamond shape problem occurs.
Then definitely next question will be what is the diamond shape problem?
Suppose we have Class A and Class A inherited in Class B and Class C, and finally when we want to inherit Class B and Class C in Class D. In this case diamond shape problem occurs. The means method from Class is overridden in Class B and Class C, when we try to inherit Class B and Class C in Class D in this case compiler will get confused about which methods need to use from Class B or Class C.
So, to overcome above said diamond shape problem we can use interfaces. And by using interfaces we can achieve multiple inheritances.
Multiple inheritance using Interfaces
We will create two interfaces with the same method declaration as below.
public interface IInterface1 {
void Display();
}
public interface IInterface2 {
void Display();
}
We will implement the above-declared interfaces to the class as below.
public class MultipleinheritDemoClass: IInterface1, IInterface2 {
void IInterface1.Display() {
Console.WriteLine("I am from Interface1 display");
}
void IInterface2.Display() {
Console.WriteLine("I am from Interface2 display");
}
}
And finally, we call methods from different interfaces which have the same definition and implementation.
IInterface1 interface1 = new MultipleinheritDemoClass();
interface1.Display();
IInterface2 interface2 = new MultipleinheritDemoClass();
interface2.Display();
Complete source code of above implementation:
namespace InterfaceDemo {
class Program {
static void Main(string[] args) {
IInterface1 interface1 = new MultipleinheritDemoClass();
interface1.Display();
IInterface2 interface2 = new MultipleinheritDemoClass();
interface2.Display();
Console.ReadKey();
}
}
public interface IInterface1 {
void Display();
}
public interface IInterface2 {
void Display();
}
public class MultipleinheritDemoClass: IInterface1, IInterface2 {
void IInterface1.Display() {
Console.WriteLine("I am from Interface1 display");
}
void IInterface2.Display() {
Console.WriteLine("I am from Interface2 display");
}
}
}
The above code is written in Visual Studio 2019 and .net framework 4.6.1 and C# version 7.3.
Since C# 8 version a new feature introduced for the interface is we can write default implementation inside the interface.
Inside the below code written in C# 8(.net 5) implemented this new feature called default method implementation in the interface.
namespace MultipleInheritance {
class Program {
static void Main(string[] args) {
IInterface1 obj1 = new MyTestClass();
obj1.display();
IInterface2 obj2 = new MyTestClass();
obj2.display();
Console.ReadKey();
}
}
public interface IInterface1 {
void display() {
Console.WriteLine("I am writing msg from ingterface1.display()");
}
}
public interface IInterface2 {
public void display();
}
public class MyTestClass: IInterface1, IInterface2 {
void IInterface2.display() {
Console.WriteLine("I am from Interface 2.Display");
}
}
}