Learn About Inheritance In C#

Introduction

In this session, we will learn about use of inheritance and advantages of inheritance, inheritance syntax, and inheritance concepts.

First, let's look at the example explaining why we need inheritance.
  1. Public class FulltimeEmployee  
  2. {  
  3.       public String First Name;  
  4.       public  String Last Name;  
  5.       public  String Email;  
  6.       public  Float yearly Salary;  
  7. Pubic void Printfullname ()  
  8.         {  
  9.         }  
  10. }  
  11.   
  12.   
  13.   
  14. Public class PartTimeEmployee  
  15. {  
  16.   public String First Name;  
  17.   public  String Last name;  
  18.   public  String Email;  
  19.   public  String Hourly Rate;  
  20.   Public void Printfullname ()  
  21.   {  
  22.   }  
  23. }   
The code between these two classes is duplicated because certain things are common in FulltimeEmployee and PartTimeEmployee class, like Firstname, Lastname, Email.  A small difference between these two is yearly Salary and Hourly Rate. Now, let's explain inheritance using these two classes in one common class Employee.
  1.       Public class Employee  
  2. {  
  3.      public String First Name;           /* Common Code moved into Base Employee class  
  4.      public String Last name;  
  5.      public String Email;  
  6.   Public void PrintFullName ()  
  7. {  
  8. }  
  9. }  
Here is the new code for FulltimeEmployee and  PartimeEmploye.
  1. Public class FulltimeEmployee ()           /* Fulltime and Part-time  Employee Specific  Code  in Respective  
  2. {                                                                            Derived classes */  
  3. Float Yearly salary;  
  4. }  
  5.    
  6. Public class ParttimeEmpoyee ()  
  7. {  
  8. Float Hourlysalaty;  
  9. }  
What is the advantage of doing this -
  • Code reused
  • A lot of coding time is saved. 
Let's now explain the code. I have created "Employee" base class where all common attributes are defined.
  1. public class FulltimeEmployee:Empolyee  
  2. {  
  3.    public float YearlySalary;  
  4.   
  5. }  
  6.   
  7. public class ParttimeEmployee:Empolyee  
  8. {  
  9.    public float HourlySalary;  
  10.   
  11. }  
What is happen  Drived Class Inherit Base Class.Derived  class  now have access to  Base class all propertise Fields.what ever Present Base class it also available Derived class .if make this base class fields public it  is available derived class .I  have create object for FullTimeEmployee and ParttimeEmployee and implemenation   to  base class Printfullname(). after Compile the code  
  1. using System;  
  2.   
  3. public class Empolyee  
  4. {  
  5.    public string Firstname;  
  6.    public string Lastname;  
  7.    public string Email;  
  8.   
  9.     public void printfulName()  
  10.     {  
  11.         Console.WriteLine(Firstname + " " + Lastname);  
  12.         Console.ReadLine();  
  13.     }  
  14. }  
  15.   
  16. public class FulltimeEmployee:Empolyee  
  17. {  
  18.    public float YearlySalary;  
  19.   
  20. }  
  21.   
  22. public class ParttimeEmployee:Empolyee  
  23. {  
  24.    public float HourlySalary;  
  25.   
  26. }  
  27.   
  28. namespace ConsoleApplication1  
  29. {  
  30.     class Program  
  31.     {  
  32.         static void Main(string[] args)  
  33.         {  
  34.            
  35.             FulltimeEmployee FTE = new FulltimeEmployee();  
  36.             FTE.Firstname = "Thenn";  
  37.             FTE.Lastname = "Arasu";  
  38.             FTE.Email = "thennarasu@gmail.com";  
  39.             FTE.YearlySalary = 5000; //Full Time Employee Specific Fields   
  40.             FTE.printfulName();    //called print the Name to printfulName base class Method
  41.   
  42.   
  43.             ParttimeEmployee PTE = new ParttimeEmployee();  
  44.             PTE.Firstname = "Karthi";  
  45.             PTE.Lastname = "Keyan";  
  46.             PTE.Email = "karthi@gmail.com";  
  47.             PTE.HourlySalary = 5000;  
  48.             PTE.printfulName();  // called print the Name  to printfulName base class  Method
  49.         }  
  50.     }  
  51. }  
OutPut Will be like this 



Why inheritance ? 

Inheritance is one of the primary pillars of object oriented prgroamming. It allows the reusing of code and reduces the coding time and error possibilities. 
 
4 primary pillars of OOPS.
  1. Inheritance 
  2. Encapsulation 
  3. Abstraction 
  4. Polymorphism

Inheritance Syntax 

  1. public class  ParentClass  
  2. {  
  3.   
  4. //Parent class implementation  
  5. }  
  6.   
  7. public class  Derviedclass : Parentclass  
  8. {  
  9.   
  10. //Child class implementation  
  11. }   
  1. In this example, the derived class inherits from parent class
  2. C# supports only single class inheritance 
  3. C# supports multiple interface inheritance 
  4. Child class is a specialization of base class
  5. Base class automatically instantiates before Derived class
  6. Parent class construcor is excuted before child class constructor 
C# does not support mulitiple class inheritance so the derived class has only one base class. Let's see an example  with code. I am trying  to add  two base classes in derived but it is showing error bacause   can not  have multiple base classes. class  have only one base class .
 
 
However, multilevel class inheritance is possible.
  1. public class ParttimeEmployee:Empolyee  
  2. {  
  3.    public float HourlySalary;  
  4.   
  5. }  
  6. public class Sample :ParttimeEmployee  
  7. {  
  8.   
  9. }  
In this ParttimeEmployee inherits from Employee Base class.  you can say  class sample inherits from ParttimeEmployee Class.Now Class Sample Wll have available   parttimeEmployee code And  Employee code also available  in sample class.Lets create sample class object  in main class We can all propertise which are available  PartimeEmployee and  Employee  base class,

 

Above Screen Created sample class object .  called S1 object name. when  try declare propertise its show  all propertise which are available in PartimeEmployee and Employee Base class. parttimeEmployee Propertise  name Hourly salary, Employee base class Propertise Firstname,Lastname,Email, printfulName  method also we can access  use sample   class object.multiple Class inheritance not  possible.  same time multiple level inheritance possible.

Parent class Constructor Called before Child Class Constructor  let say example with Code   
  1. using System;  
  2.   
  3. public class parentclass  
  4. {  
  5.   public parentclass()  
  6.     {  
  7.         Console.WriteLine("Parentclass Constructor called");  
  8.     }  
  9. }  
  10.   
  11. public class Childclass :parentclass  
  12. {  
  13.     public Childclass()  
  14.     {  
  15.         Console.WriteLine("Childclass constructor Called");  
  16.     }  
  17. }  
  18.   
  19. namespace ConsoleApplication1  
  20. {  
  21.     class Program  
  22.     {  
  23.         static void Main(string[] args)  
  24.         {  
  25.             //Now create object child Class  
  26.             Childclass cc = new Childclass();  
  27.             Console.ReadLine();  
  28.         }  
  29.     }  
  30. }  
in this code we have two class one parent class and other child class when create object of child class Parent Class will called Automatically  Now Run this Code output  Will be Like this 
 
 
 
In other hand in parent class have another  constructor its overload constructor . 
  1. using System;  
  2.   
  3. public class parentclass  
  4. {  
  5.   public parentclass()  
  6.     {  
  7.         Console.WriteLine("Parentclass Constructor called");  
  8.     }  
  9.     //Constructor overload   if you want  call constructor  overload we use base keyword   
  10.     public parentclass( string overload)  
  11.     {  
  12.         Console.WriteLine(overload); // lets print the message   
  13.     }  
  14. }  
  15.   
  16. public class Childclass :parentclass  
  17. {  
  18.     public Childclass() :base("Derived  Class Control Parent class")  
  19.     {  
  20.         Console.WriteLine("Childclass constructor Called");  
  21.     }  
  22. }  
  23.   
  24. namespace ConsoleApplication1  
  25. {  
  26.     class Program  
  27.     {  
  28.         static void Main(string[] args)  
  29.         {  
  30.             //Now create object child Class  
  31.             Childclass cc = new Childclass();  
  32.             Console.ReadLine();  
  33.         }  
  34.     }  
  35. }  
Result Will like this Parent class overload result will First,

 
 
overload constructor i have passed something like  i  pass string datatype    name called  overload . in parent class has two constructor  one Which takes parameter and another one which does not take parameter  lets Going To run  the Code parameter less Constructor Will  called  first. if you want  run the  constructor overload  string message  childclass we use base keyword  use  control the Parent class.

Conculsion

In this Article  I hope you Understand  Inheritance ,Inheritance Syntax,Inheritance Concepts. How to  us Multilevel class inheritance also Discuss about  derived classa and base class.  please share all your feedback and comments to improve my future article.
X

Build smarter apps with Machine Learning, Bots, Cognitive Services - Start free.

Start Learning Now