In c# multi-level inheritance is possible but multiple inheritance is not possible because it will make code more complex. In case if you to implement multiple inheritance in your application we can achieve that by using interfaces.
We will see how to achieve multiple inheritance in c# using interfaces with example.
Example of Multiple Inheritance
Following is the example of implementing multiple inheritance in c# applications using interface.
using System;
namespace inheritanceexample
{
class Program
{
static void Main(string[] args)
{
Operations op = new Operations();
Console.WriteLine("Multiple inheritance using interface\n ");
Console.WriteLine("Result1: " + op.method1(20, 10));
Console.WriteLine("Result2: " + op.method2(100, 50));
Console.WriteLine("Result3: " + op.method3(30, 3));
Console.ReadLine();
}
}
class Operations : inface1, inface2, inface3
{
public int method1(int g, int h)
{
return g + h;
}
public int method2(int i, int j)
{
return i - j;
}
public int method3(int k, int l)
{
return k * l;
}
}
interface inface1
{
int method1(int a, int b);
}
interface inface2
{
int method2(int c, int d);
}
interface inface3
{
int method3(int e, int f);
}
}
|
Output of Multiple Inheritance Example
Following is the result of multiple inheritance example.
I hope it helps you to understand how to achieve multiple inheritance in c# using interface.



0 comments:
Post a Comment
Note: only a member of this blog may post a comment.