Before reading this article, I highly recommend reading the previous part of the series,
Inheritance
Inheritance is a property of OOPs (Object Oriented Programming) language. that is used to derive new class from already existing class. There are 5 types of Inheritance,
- Single Inheritance
- Multilevel Inheritance
- Multiple Inheritance
- Hybrid Inheritance
- Hierarchical Inheritance
Multiple Inheritance
In Multiple Inheritance one new class get derived from two base classes as in the following figure,
Figure: Multiple Inheritance
The above example show that there is one class Class C that gets derived from two base classes Class A and Class B.
Example
- Class UGC
- String UgcCode, UgcAdress;
- Public void Get()
- {
- Console.WriteLine(“Enter the Ugc Code and Adress: ”);
- This.UgcCode = Console.ReadLine();
- This.UgcAdress = Console.ReadLine();
- }
- Public void display()
- {
- Console.WriteLine(“UGC Code is: ”+This.UgcCode);
- Console.WriteLine(“UGC Office Adress is: ”+This.UgcAdress);
- }
- Class university: UGC
- String ucode, Uname, UAddress, Utype
- Public void Get()
- {
- Console write(“enter the university Code, Name, Adress and Type details: ”);
- This UCode = console Readline();
- This UName = console Readline();
- This UAddress = console Readline();
- This Utype = Console readline();
- }
- Public void Display()
- {
- Console.WriteLine(“University Code is: ”+This.UCode);
- Console.WriteLine(“University Name is: ”+This.UName);
- Console.WriteLine(“University Adress is: ”+This.UAdress);
- Console.WriteLine(“University Type is: ”+This.UType);
- }
- Class Class: University
- String Ccode, Cname, CAddress, Ctype
- Public void Get()
- {
- Console write(“enter the College Code, Name, Adress and Type details);
- This CCode = console Readline();
- This CName = console Readline();
- This CAddress = console Readline();
- This Ctype = Console readline();
- }
- Public void Display()
- {
- Console.WriteLine(“University Code is: ”+This.CCode);
- Console.WriteLine(“University Name is: ”+This.CName);
- Console.WriteLine(“University Adress is: ”+This.CAdress);
- Console.WriteLine(“University Type is: ”+This.CType);
- }
- Class InterfaceImplimentation
- Public void Main()
- {
- College obj = new College();
- Obj.Get();
- Obj.Display();
- }
- }
- Console.ReadLine();
- }
Point to Notice: In C# .NET we use interface to implement multiple inheritance.
Interface Interface is used to control the class and it do not contain any member variable and it doesn’t implement any method too. But the methods created by interface will get implemented by Base class and as well as child class too. For implementing Multiple inheritance in C#.NET their should be at least one interface as base class,
Figure: Multiple Inheritance with interface
In the above figure one new class C get derived from Base class A and Interface I1.
Example: - Interface UGC
- {
- Public void Get()
- Public void display()
- }
- Class university: UGC
- String ucode, Uname, UAddress, Utype
- Public void Get()
- {
- Console write(“enter the university details);
- This UCode = console Readline();
- This UName = console Readline();
- This UAddress = console Readline();
- This Utype = Console readline();
- }
- Public void Display()
- {
- Console.WriteLine(“University Code is” + This.UCode);
- Console.WriteLine(“University Name is” + This.UName);
- Console.WriteLine(“University Adress is” + This.UAdress);
- Console.WriteLine(“University Type is” + This.UType);
- }
- Class Class: University
- String Ccode, Cname, CAddress, Ctype
- Public void Get()
- {
- Console write(“enter the College, details);
- This CCode = console Readline();
- This CName = console Readline();
- This CAddress = console Readline();
- This Ctype = Console readline();
- }
- Public void Display()
- {
- Console.WriteLine(“University Code is” + This.CCode);
- Console.WriteLine(“University Name is” + This.CName);
- Console.WriteLine(“University Adress is” + This.CAdress);
- Console.WriteLine(“University Type is” + This.CType);
- }
- Class InterfaceImplimentation
- Public void Main()
- {
- College obj = new College();
- Obj.Get();
- Obj.Display();
- }
- }
- Console.ReadLine();
- }
Point To Remember For implementing Multiple Inheritance we use minimum one Interface as base class. Interface is used to control the class which does not contain any member variable and does not implement anything. But the methods which get created by interface will be implemented by Class.