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.
- Public class FulltimeEmployee
- {
- public String First Name;
- public String Last Name;
- public String Email;
- public Float yearly Salary;
- Pubic void Printfullname ()
- {
- }
- }
-
-
-
- Public class PartTimeEmployee
- {
- public String First Name;
- public String Last name;
- public String Email;
- public String Hourly Rate;
- Public void Printfullname ()
- {
- }
- }
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.
- Public class Employee
- {
- public String First Name; /* Common Code moved into Base Employee class
- public String Last name;
- public String Email;
- Public void PrintFullName ()
- {
- }
- }
Here is the new code for FulltimeEmployee and PartimeEmploye.
- Public class FulltimeEmployee ()
-
- Float Yearly salary;
- }
-
- Public class ParttimeEmpoyee ()
- {
- Float Hourlysalaty;
- }
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.
- public class FulltimeEmployee:Empolyee
- {
- public float YearlySalary;
-
- }
-
- public class ParttimeEmployee:Empolyee
- {
- public float HourlySalary;
-
- }
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
- using System;
-
- public class Empolyee
- {
- public string Firstname;
- public string Lastname;
- public string Email;
-
- public void printfulName()
- {
- Console.WriteLine(Firstname + " " + Lastname);
- Console.ReadLine();
- }
- }
-
- public class FulltimeEmployee:Empolyee
- {
- public float YearlySalary;
-
- }
-
- public class ParttimeEmployee:Empolyee
- {
- public float HourlySalary;
-
- }
-
- namespace ConsoleApplication1
- {
- class Program
- {
- static void Main(string[] args)
- {
-
- FulltimeEmployee FTE = new FulltimeEmployee();
- FTE.Firstname = "Thenn";
- FTE.Lastname = "Arasu";
- FTE.Email = "[email protected]";
- FTE.YearlySalary = 5000;
- FTE.printfulName(); //called print the Name to printfulName base class Method
-
-
- ParttimeEmployee PTE = new ParttimeEmployee();
- PTE.Firstname = "Karthi";
- PTE.Lastname = "Keyan";
- PTE.Email = "[email protected]";
- PTE.HourlySalary = 5000;
- PTE.printfulName(); // called print the Name to printfulName base class Method
- }
- }
- }
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.
- Inheritance
- Encapsulation
- Abstraction
- Polymorphism
Inheritance Syntax
- public class ParentClass
- {
-
-
- }
-
- public class Derviedclass : Parentclass
- {
-
-
- }
- In this example, the derived class inherits from parent class
- C# supports only single class inheritance
- C# supports multiple interface inheritance
- Child class is a specialization of base class
- Base class automatically instantiates before Derived class
- 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.
- public class ParttimeEmployee:Empolyee
- {
- public float HourlySalary;
-
- }
- public class Sample :ParttimeEmployee
- {
-
- }
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
- using System;
-
- public class parentclass
- {
- public parentclass()
- {
- Console.WriteLine("Parentclass Constructor called");
- }
- }
-
- public class Childclass :parentclass
- {
- public Childclass()
- {
- Console.WriteLine("Childclass constructor Called");
- }
- }
-
- namespace ConsoleApplication1
- {
- class Program
- {
- static void Main(string[] args)
- {
-
- Childclass cc = new Childclass();
- Console.ReadLine();
- }
- }
- }
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 .
- using System;
-
- public class parentclass
- {
- public parentclass()
- {
- Console.WriteLine("Parentclass Constructor called");
- }
-
- public parentclass( string overload)
- {
- Console.WriteLine(overload);
- }
- }
-
- public class Childclass :parentclass
- {
- public Childclass() :base("Derived Class Control Parent class")
- {
- Console.WriteLine("Childclass constructor Called");
- }
- }
-
- namespace ConsoleApplication1
- {
- class Program
- {
- static void Main(string[] args)
- {
-
- Childclass cc = new Childclass();
- Console.ReadLine();
- }
- }
- }
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.