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. Public class PartTimeEmployee
  12. {
  13. public String First Name;
  14. public String Last name;
  15. public String Email;
  16. public String Hourly Rate;
  17. Public void Printfullname ()
  18. {
  19. }
  20. }
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. Public class ParttimeEmpoyee ()
  6. {
  7. Float Hourlysalaty;
  8. }
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. public class ParttimeEmployee:Empolyee
  6. {
  7. public float HourlySalary;
  8. }
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. public class Empolyee
  3. {
  4. public string Firstname;
  5. public string Lastname;
  6. public string Email;
  7. public void printfulName()
  8. {
  9. Console.WriteLine(Firstname + " " + Lastname);
  10. Console.ReadLine();
  11. }
  12. }
  13. public class FulltimeEmployee:Empolyee
  14. {
  15. public float YearlySalary;
  16. }
  17. public class ParttimeEmployee:Empolyee
  18. {
  19. public float HourlySalary;
  20. }
  21. namespace ConsoleApplication1
  22. {
  23. class Program
  24. {
  25. static void Main(string[] args)
  26. {
  27. FulltimeEmployee FTE = new FulltimeEmployee();
  28. FTE.Firstname = "Thenn";
  29. FTE.Lastname = "Arasu";
  30. FTE.Email = "[email protected]";
  31. FTE.YearlySalary = 5000; //Full Time Employee Specific Fields
  32. FTE.printfulName(); //called print the Name to printfulName base class Method
  33. ParttimeEmployee PTE = new ParttimeEmployee();
  34. PTE.Firstname = "Karthi";
  35. PTE.Lastname = "Keyan";
  36. PTE.Email = "[email protected]";
  37. PTE.HourlySalary = 5000;
  38. PTE.printfulName(); // called print the Name to printfulName base class Method
  39. }
  40. }
  41. }
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. //Parent class implementation
  4. }
  5. public class Derviedclass : Parentclass
  6. {
  7. //Child class implementation
  8. }
  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. public class Sample :ParttimeEmployee
  6. {
  7. }
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. public class parentclass
  3. {
  4. public parentclass()
  5. {
  6. Console.WriteLine("Parentclass Constructor called");
  7. }
  8. }
  9. public class Childclass :parentclass
  10. {
  11. public Childclass()
  12. {
  13. Console.WriteLine("Childclass constructor Called");
  14. }
  15. }
  16. namespace ConsoleApplication1
  17. {
  18. class Program
  19. {
  20. static void Main(string[] args)
  21. {
  22. //Now create object child Class
  23. Childclass cc = new Childclass();
  24. Console.ReadLine();
  25. }
  26. }
  27. }
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. public class parentclass
  3. {
  4. public parentclass()
  5. {
  6. Console.WriteLine("Parentclass Constructor called");
  7. }
  8. //Constructor overload if you want call constructor overload we use base keyword
  9. public parentclass( string overload)
  10. {
  11. Console.WriteLine(overload); // lets print the message
  12. }
  13. }
  14. public class Childclass :parentclass
  15. {
  16. public Childclass() :base("Derived Class Control Parent class")
  17. {
  18. Console.WriteLine("Childclass constructor Called");
  19. }
  20. }
  21. namespace ConsoleApplication1
  22. {
  23. class Program
  24. {
  25. static void Main(string[] args)
  26. {
  27. //Now create object child Class
  28. Childclass cc = new Childclass();
  29. Console.ReadLine();
  30. }
  31. }
  32. }
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.