INTERFACE ALLOWS PERFORMING MULTIPLE INHERITANCES IN C#
In c #sharp normally we can’t perform the multiple inheritances as we done single or multilevel inheritance the compiler restricted to do such a process. But by using interface we are able to perform multiple inheritances that is explaining with the example below.POINT TO PERFORM MULTIPLE INHERITANCES
When we going to perform multiple inheritance we should keep on eye to the following points.1. In case of base class during inheriting the base class must come first before the interfaces. See example
2. We can perform multiple inheritances of interfaces, in that case no matter which interface come first.
3. The implementation of all interface abstract member can be done in a child class.
SYNTAX
[<Modifier>] Interface <IName1>{
Abstract members; //but here we need not declare the members as abstract and public b,c
by default all member will be public and abstract
}
[<Modifier>] Interface <IName2>
{
Abstract members;
}
[<Modifier>] class <Name>: <IName1>, <IName2>
{
Implementation members;
}
EXAMPLES
class Program{
static void Main(string[] args)
{
Implementation Obj = new Implementation ();
Obj.Add(12, 34, 67);
Obj.Sub(34, 76);
interface allow us to perform multiple inheritance in c# |
Obj.Div(122, 22);
Obj.Rem(35, 4);
Console.ReadKey();
}
}
public interface ITest
{
void Add(int a, int b, int c);
void Sub(int a, int b);
}
public interface ITest1
{
void Rem(int a, int b);
}
public class Test2
{
public void Mul(int a, int b)
{
Console.WriteLine(a * b);
}
public void Div(int a, int b)
{
Console.WriteLine(a / b);
}
}
public class Implementation: Test2, ITest1, ITest
{
public void Add(int a, int b, int c)
{
Console.WriteLine(a + b + c);
}
public void Sub(int a, int b)
{
Console.WriteLine(a - b);
}
public void Rem(int a, int b)
{
Console.WriteLine(a / b);
}
}