Skip to main content

Inheritance in c#

Inheritance is one of the pillars of Object Oriented Programming. It allows the child class to reuse, extend and modify the behavior of parent class. The class whose members are inherited is called as base class and class who inherits is called a derived class. We can inherit only one class at a time. Multiple inheritance is not supported in c#. A struct cannot inherit any base class doing so will generate a compiler error.
1.      Private members are only accessible to the derived class which is nested in the inherited base class.
2.      Protected members are accessible only in derived class.
3.      Internal members are accessible in the derived class that is visible in the same assembly of the base class.
4.      Public members are accessible in the derived class. They can be called just as if they were declared in the derived class.
All Types are implicitly inherited by System.Object, this ensures that some common functionality is available to any type. Below are examples of common functionality
1.      toString(): To convert any object in its string representation.
2.      Public instance Equals(object), public static Equals(object,object) , Public static referenceEquals(Object,Object): To check equality of two objects.
3.      GetHashCode: Value of the type that can be used in hash collections.
4.      GetType: Return TYPE the object represents.
Virtual Keyword:
To override a method from base class we need to mark that method as virtual. If it is not marked as virtual then we cannot override the method, on attempting to override non- virtual, non-abstract method the compiler will throw a compile-time error.





Abstract Keyword:
When a method is declared as abstract in the base class, it is compulsory for derived to override the abstract method else a compiler error will be generated.
Inheritance represents “Is a“ relationship between derived class and base class.
A struct cannot inherit a class compiler generates an error.

 

Below is the example of inheritance.

Child class uses a Base keyword to pass data to the parent class and on the data passed to the base class, it forms a string with employee id and employee name. Classes Admin, Engineer, Finance, inherit from class Employee. Class employees have public Properties Employee name and employee Id. The class employee also overrides tostring() method of base class Object(This is an example of implicit inheritance).


 public class Employee  
   {  
     public Employee(int employeeId, string employeeName)  
     {  
       EmployeeId = employeeId;  
       EmployeeName = employeeName;  
     }  
     public int EmployeeId { get; set; }  
     public string EmployeeName { get; set; }  
     public override string ToString() => $"{EmployeeId} : {EmployeeName}";  
   }  
   public class Admin : Employee  
   {  
     //Admin class uses base keyword to pass data to parent class.  
     public Admin(int employeeId,string employeeName): base(employeeId, employeeName)  
     {  
     }  
   }  
   public class Finance:Employee  
   {  
     //Finance class uses base keyword to pass data to parent class.  
     public Finance(int employeeId, string employeeName): base(employeeId, employeeName)  
     {  
     }  
   }  
   public class Engineer : Employee  
   {  
     //Engineer class uses base keyword to pass data to parent class.  
     public Engineer(int employeeId, string employeeName): base(employeeId, employeeName)  
     {  
     }  
   }  
  class Program  
   {  
     static void Main(string[] args)  
     {  
       Engineer emp = new Engineer(1,"Rajesh");  
       Console.WriteLine($"{emp.ToString()} : {typeof(Engineer).Name}");  
       Finance finemp = new Finance(2, "Vishwanath");  
       Console.WriteLine($"{finemp.ToString()} : {typeof(Finance).Name}");  
       Admin admEmp = new Admin(3, "Amit");  
       Console.WriteLine($"{admEmp.ToString()} : {typeof(Admin).Name}");  
       List<Employee> lstEmployeees = new List<Employee>()  
       {  
         emp,admEmp,finemp  
       };  
       Console.WriteLine("Count of employees: "+ lstEmployeees.Count);  
       Console.ReadKey();  
     }  
   }  

Comments