Multiple Inheritance in C#


In this article we are going to see, Inheritance in C#. So, What is an Inheritance in C#? An Inheritance is a concept or approach where one type can be derived from other types Just like a son gets his DNA from his parents. In c#, we can derive a type from other types. So the child type can inherit its parent type members such as Methods, Property, etc. Now, we will see Multiple Inheritance where a class can derive from more than one type. But for that, a class has to derive from Interfaces. Because Interfaces supports multiple Inheritance, a normal class derived from only one Class and multiple interfaces. (Type means Classes, Interfaces, and Abstract classes)Following is a simple program to understand it.

Code Snippet:-

public interface Interface1
{
/// <summary>
/// Abstract method declaration
/// </summary>
/// <param name="_input"></param>
void WhatUgot(int _input);
}

public interface Interface2
{
/// <summary>
/// Abstract method declaration
/// </summary>
/// <param name="_input"></param>
void WhatUgive(int _input);
}

/// <summary>
/// Non Abstract Class derived from Interface1 and Interface2
/// </summary>
public class ClassMulti : Interface1, Interface2
{
///// <summary>
///// Inherited Mthod from Interface1
///// </summary>
///// <param name="_input"></param>
public void WhatUgot(int _input)
{
Console.WriteLine("This is Overriden Method");
}

/// <summary>
/// Inherited Method from Interface2
/// </summary>
/// <param name="_input"></param>
public void WhatUgive(int _input)
{
Console.WriteLine("This is Overriden Method");
}
}

public class MultipleInheritance
{
public static void Main()
{
ClassMulti obj = new ClassMulti();
obj.WhatUgot(1);
obj.WhatUgive(1);
Console.ReadLine();
}
}


In the above example, We have derived “ClassMulti” from two Interfaces and inherited their
methods that’s why we called it Multiple Inheritance. But this is not possible for multiple Classes.

Comments

Unknown said…
Very nice tips for biginers
Unknown said…
Very nice tips for biginers
Manusha said…
This comment has been removed by a blog administrator.

Popular posts from this blog

How to Transpose DataTable in C# ?

Process Template Editor Extension Tool in Visual Studio Integration Tutorial