Inheritance In C# - Part 2

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,

  1. Single Inheritance
  2. Multilevel Inheritance
  3. Multiple Inheritance
  4. Hybrid Inheritance
  5. Hierarchical Inheritance

Multiple Inheritance

In Multiple Inheritance one new class get derived from two base classes as in the following figure,

Multiple Inheritance
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

  1. Class UGC  
  2. String UgcCode, UgcAdress;  
  3. Public void Get()  
  4. {  
  5.     Console.WriteLine(“Enter the Ugc Code and Adress: ”);  
  6.     This.UgcCode = Console.ReadLine();  
  7.     This.UgcAdress = Console.ReadLine();  
  8. }  
  9. Public void display()  
  10. {  
  11.     Console.WriteLine(“UGC Code is: ”+This.UgcCode);  
  12.     Console.WriteLine(“UGC Office Adress is: ”+This.UgcAdress);  
  13. }  
  14. Class university: UGC  
  15. String ucode, Uname, UAddress, Utype  
  16. Public void Get()  
  17. {  
  18.     Console write(“enter the university Code, Name, Adress and Type details: ”);  
  19.     This UCode = console Readline();  
  20.     This UName = console Readline();  
  21.     This UAddress = console Readline();  
  22.     This Utype = Console readline();  
  23. }  
  24. Public void Display()  
  25. {  
  26.     Console.WriteLine(“University Code is: ”+This.UCode);  
  27.     Console.WriteLine(“University Name is: ”+This.UName);  
  28.     Console.WriteLine(“University Adress is: ”+This.UAdress);  
  29.     Console.WriteLine(“University Type is: ”+This.UType);  
  30. }  
  31. Class Class: University  
  32. String Ccode, Cname, CAddress, Ctype  
  33. Public void Get()  
  34. {  
  35.     Console write(“enter the College Code, Name, Adress and Type details);  
  36.     This CCode = console Readline();  
  37.     This CName = console Readline();  
  38.     This CAddress = console Readline();  
  39.     This Ctype = Console readline();  
  40. }  
  41. Public void Display()  
  42. {  
  43.     Console.WriteLine(“University Code is: ”+This.CCode);  
  44.     Console.WriteLine(“University Name is: ”+This.CName);  
  45.     Console.WriteLine(“University Adress is: ”+This.CAdress);  
  46.     Console.WriteLine(“University Type is: ”+This.CType);  
  47. }  
  48. Class InterfaceImplimentation  
  49. Public void Main()  
  50. {  
  51.     College obj = new College();  
  52.     Obj.Get();  
  53.     Obj.Display();  
  54. }  
  55. }  
  56. Console.ReadLine();  
  57. }  
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, 

Multiple Inheritance with Interface
Figure: Multiple Inheritance with interface

In the above figure one new class C get derived from Base class A and Interface I1.

Example:
  1. Interface UGC  
  2. {  
  3.     Public void Get()  
  4.     Public void display()  
  5. }  
  6. Class university: UGC  
  7. String ucode, Uname, UAddress, Utype  
  8. Public void Get()  
  9. {  
  10.     Console write(“enter the university details);  
  11.     This UCode = console Readline();  
  12.     This UName = console Readline();  
  13.     This UAddress = console Readline();  
  14.     This Utype = Console readline();  
  15. }  
  16. Public void Display()  
  17. {  
  18.     Console.WriteLine(“University Code is” + This.UCode);  
  19.     Console.WriteLine(“University Name is” + This.UName);  
  20.     Console.WriteLine(“University Adress is” + This.UAdress);  
  21.     Console.WriteLine(“University Type is” + This.UType);  
  22. }  
  23. Class Class: University  
  24. String Ccode, Cname, CAddress, Ctype  
  25. Public void Get()  
  26. {  
  27.     Console write(“enter the College, details);  
  28.     This CCode = console Readline();  
  29.     This CName = console Readline();  
  30.     This CAddress = console Readline();  
  31.     This Ctype = Console readline();  
  32. }  
  33. Public void Display()  
  34. {  
  35.     Console.WriteLine(“University Code is” + This.CCode);  
  36.     Console.WriteLine(“University Name is” + This.CName);  
  37.     Console.WriteLine(“University Adress is” + This.CAdress);  
  38.     Console.WriteLine(“University Type is” + This.CType);  
  39. }  
  40. Class InterfaceImplimentation  
  41. Public void Main()  
  42. {  
  43.     College obj = new College();  
  44.     Obj.Get();  
  45.     Obj.Display();  
  46. }  
  47. }  
  48. Console.ReadLine();  
  49. }  
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.

 


Recommended Free Ebook
Similar Articles