- C#.Net doesn’t support multiple inheritance, by using two or more bases classes, like
- To implement multiple inheritance, there should be one or none base classes and should be one or more interfaces
- When we implement multiple inheritance using a class and an interface then, first mention class name then interface name like…
class
C: A, B //A is Class Name and B is Interface name
{
Code…
}
- But do not write Like
class
C: B, A //A is Class Name and B is Interface name
{
Code…
}
- This will raise Compilation error
Why C#.NET does not Supports MultipleInheritance
- C#.NET doesn’t support Multiple Inheritance using classes because:
- If we consider two classes C1,C2 like…
class
C1
{
public void F1( )
{
Code…
}
}
class
C2
{
public void F1( )
{
Code…
}
}
- And create a new class C3 like
class
C3:C1, C2
{
Code…
}
- Then create object for class C3 like
C3
obj1=new C3 ()
- And if we call function F1( ) like…
Obj.F1(
);
- Then runtime will get confusion to call function F1( ) of class C1 or C2
- This situation is known as Ambiguity situation, So C#.Net doesn’t support multiple inheritance using 2 or more classes as base level
Example:-
Class Diagram:-
using System;
using
System.Collections.Generic;
using System.Linq;
using System.Text;
//Program
to implement Multiple Inheritance
namespace CAMLInheritance
{
class ClsBranch
{
protected
int BCode;
protected
string BName;
protected
string BAddress;
public
void GetBData()
{
Console.WriteLine("Enter the BCode,BName,BAddress:- ");
BCode = Convert.ToInt32(Console.ReadLine());
BName = Console.ReadLine();
BAddress = Console.ReadLine();
}
public
void DisplayBData()
{
Console.WriteLine("Branch Details Are:- ");
Console.WriteLine("Branch code is :- "+BCode);
Console.WriteLine("Branch Name is :- "+BName);
Console.WriteLine("Branch Address is :-
"+BAddress);
}
}
interface
ClsEmployee
{
void
GetEmpData();
void
DisplayEmpData();
}
class ClsManager:ClsBranch,ClsEmployee
{
int
EmpId;
string
EName;
double
Bonus;
double
Ca;
public
void GetEmpData()
{
Console.WriteLine("Enter the Manager Id,Name,Bonus,Ca");
EmpId = Convert.ToInt32(Console.ReadLine());
EName = Console.ReadLine();
Bonus = Convert.ToInt32(Console.ReadLine());
Ca = Convert.ToInt32(Console.ReadLine());
}
public
void DisplayEmpData()
{
Console.WriteLine("Manager Detatails Are :-
");
Console.WriteLine("Manager Id is :- " + EmpId);
Console.WriteLine("Manager Name is
:- " + EName);
Console.WriteLine("Manager Bonus is :- "+Bonus);
Console.WriteLine("Manager Ca is :- "+Ca);
}
}
class ClsMPLInheritance
{
static
void Main(string[]
args)
{
ClsManager
obj1 = new ClsManager();
obj1.GetEmpData();
obj1.GetBData();
obj1.DisplayEmpData();
obj1.DisplayBData();
Console.ReadLine();
}
}
}
|
Output :-
No comments:
Post a Comment