Introduction:
Here I will explain how to achieve multiple inheritance in c# with interface example or implement multiple inheritance in c# with example or use interface to implement multiple inheritances in c#, vb.net with example. In c# multiple inheritance can be achieved by using interfaces.
Description:
In
previous posts I explained dictionary object in c#, vb.net, difference between dispose and finalize method in c#, vb.net,
difference between ref and out in c#, vb.net with example, constructors in c#, vb.net with example, difference
between char varchar nvarchar in sql, sealed classes in c#, vb.net with example and many articles related to interview questions, asp.net, c#.net. Now I will explain how to
achieve multiple inheritance in c# using interface with example.
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.
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.
If you enjoyed this post, please support the blog below. It's FREE! Get the latest Asp.net, C#.net, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email. |
|||
|
|||
0 comments :
Note: Only a member of this blog may post a comment.