Inheritance In C# - Part 1

Inheritance

Inheritance is a property of OOP (Object Oriented Programming ) language which is used to derive new class from already existed class. There are 5 types of Inheritance,

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

Single Inheritance

In single inheritance one new class get derived from already existing Base class as in the following figure,

Single Inheritance
Figure: Single Inheritance

The above example has shown there is one Class A which is base class and another class is Class B which get derived from Class A.

Example

Class Employee

  1. Int Id;  
  2. String Name;  
  3. Public void Get()  
  4. {  
  5.     Console.WriteLine(“Enter the Details of Employee“)  
  6.     This.Id = Convert.ToInt32(Console.ReadLine());  
  7.     This.Name = Console.ReadLine();  
  8. }  
  9. Public void Display()  
  10. {  
  11.     Console.WriteLine(“Id is: ”+This.Id);  
  12.     Console.WriteLine(“Name is: ”+This.Name);  
  13. }  
  14. Class Branch: Employee  
  15. String Name, Adress;  
  16. Public void Get()  
  17. {  
  18.     Console.WriteLine(“Enter the Branch Value“)  
  19.     This.Name = Console.ReadLine();  
  20.     This.Adress = Console.ReadLine();  
  21. }  
  22. Public void Display()  
  23. {  
  24.     Console.WriteLine(“Name is: ”+This.Name);  
  25.     Console.WriteLine(“Name is: ”+This.Adress);  
  26. }  
  27. Class SinInheritance  
  28. Public Void Main()  
  29. {  
  30.     Branch Obj = new Branch();  
  31.     Obj.Get();  
  32.     Obj.Display();  
  33. }  
  34. }  
Multilevel Inheritance

Multilevel Inheritance

Figure: Multilevel Inheritance

In Multilevel inheritance one new class get derived from Base class and another class will get derived from class which has recently derived from base class as shown in above figure. As we can see there are 3 classes Class A ,Class B and Class C in which Class A is Parent class of Class B and Class B is Parent class of Class C.

Example

Class Employee.
  1. Int Id;  
  2. String Name;  
  3. Public void Get()  
  4. {  
  5.     Console.WriteLine(“Enter the Details of Employee“)  
  6.     This.Id = Convert.ToInt32(Console.ReadLine());  
  7.     This.Name = Console.ReadLine();  
  8. }  
  9. Public void Display()  
  10. {  
  11.     Console.WriteLine(“Id is: ”+This.Id);  
  12.     Console.WriteLine(“Name is: ”+This.Name);  
  13. }  
  14. Class Branch: Employee  
  15. String BName, Adress;  
  16. Public void Get()  
  17. {  
  18.     Console.WriteLine(“Enter the Branch Details“)  
  19.     This.BName = Console.ReadLine();  
  20.     This.Adress = Console.ReadLine();  
  21. }  
  22. Public void Display()  
  23. {  
  24.     Console.WriteLine(“Name is: ”+This.BName);  
  25.     Console.WriteLine(“Name is: ”+This.Adress);  
  26. }  
  27. Class Salary: Branch  
  28. Double Salaries;  
  29. Public Void Get()  
  30. {  
  31.     Console.WriteLine(“Enter The Salary Details: ”);  
  32.     This.Salaries = Convert.ToDouble(Console.ReadLine());  
  33. }  
  34. Public Void Display()  
  35. {  
  36.     Console.WriteLine(“Salary is: ”+This.Salaries);  
  37. }  
  38. Class MultiLevelInheritance  
  39. Public Void Main()  
  40. {  
  41.     Salary Obj = new Salary();  
  42.     Obj.Get();  
  43.     Obj.Display();  
  44. }  
  45. }  
Point To Remember

Inheritance is one of the property of oops which is used to derive new class from already existing class.

 


Similar Articles