Home | | C Sharp and .NET Framework(CNF) | Inheritance in C#

Chapter: C# and .NET Framework

Inheritance in C#

There are 5 types of inheritance as shown below. · Single Inheritance · Multilevel Inheritance · Multiple Inheritance · Hierarchical Inheritance · Hybrid Inheritance

INHERITANCE IN C#.

 

Inheritance means parent-child relationship.By using Inheritance methodology we can create a new class by using existing class code (i.e. reuse existing methods, properties etc).It also referred to as reusability of the code so by using Inheritance we can reuse the code again and again.

 

What We    Call

 

In Inheritance main existing class is called as generalized class, base class, super class and parent class and the new class created from existing class is called as specialized class, sub class, child class and derived class. We normally talk in terms of base class and derived class.

 

Syntax of Inheritance

class ParentClass{

 

...parent class code

}

class ChildClass : ParentClass{

 

...child class code

}

Special Character ":" in Inheritance

 Inheritance uses special character called ":" colon to make a relationship between parent and child as you can see in above syntax of code.

When to Implement Interitance

 When we create a new class and we want to reuse some of the methods or properties from existing class then that is an ideal time to implement inheritance.

Advantage of Interitance

Reusability of the code.

 

Types of inheritance in c#

There are 5 types of inheritance as shown below.

 

·        Single Inheritance

·        Multilevel Inheritance

·        Multiple Inheritance

·        Hierarchical Inheritance 

·        Hybrid Inheritance

Single Inheritance

 

Single Inheritance means when a single base is been implemented to single derived class is called as Single Inheritance. Means we have only one parent class and one child class.


 

Example of Single Inheritance 

class Company{

 

public void CompanyName(){ Console.WriteLine("Name of the Company");

 

}

 

public void CompanyAddress(){ Console.WriteLine("Address of the Company");

}

}

 

class Employee : Company { public void NameofEmployee(){

Console.WriteLine("Name of the Employee");

 

}

 

public void Salary(){ Console.WriteLine("Salary of the Employee");

 

}

}

 

Multilevel Inheritance

 

When a derived class is created from another derived class or let me put it in this way that a class is created by using another derived class and this type of implementation is called as multilevel Inheritance


Example of Multilevel Inheritance

class HeadOffice

 

{

 

public void HeadOfficeAddress(){ Console.WriteLine("Head Office Address");

}

}

 

Now let's assume that class "HeadOffice" is our Parent class or Base class. Now in my next step i will create one derived class.

 

class BranchOffice : HeadOffice{

 

public void BranchOfficeAddress(){ Console.WriteLine("Branch Office Address");

}

 

}

So as you can see that i have created a derived class "BranchOffice" by implementing the class "HeadOffice" to it.

 

It means now we have one parent class and one derived class. In the next step i will create another derived class by implementing our existing derived class "BranchOffice" to achieve multilevel Inheritance.

 

class Employee : BranchOffice {

 

public void NameofEmployee(){ Console.WriteLine("Name of the Employee");

 

}

 

public void Salary(){ Console.WriteLine("Salary of the Employee");

 

}

}

 

From the above souce code you can see that we have achieved multilevel Inheritance by implementing one derived class to another derived class. Now the class "Employee" will have the access of all the properties and methods of class "BranchOffice" and class "HeadOffice".

Multiple Inheritance

 

Due to the complexity of a code multiple inheritance is not been supported in C# or in DOT.NET but DOT.NET or C# supports multiple interfaces.

 

Hierarchical Inheritance

 

When more than one derived classes are implemented from a same parent class or base class then that type of implementation is known as hierarchical inheritance.


In short it means single base class having more than one derived classes. class HeadOffice

 

{

public void HeadOfficeAddress()

{

Console.WriteLine("Head Office Address");

}

}

class BranchOffice1 : HeadOffice

{

public void BranchOfficeAddress()

{

Console.WriteLine("Branch Office Address");

}

}

class BranchOffice2 : HeadOffice

{

public void BranchOfficeAddress()

{

Console.WriteLine("Branch Office Address");

}

}

 

As you can see from above the code that we have one base class "HeadOffice" and two derived classes "BranchOffice1" and "BranchOffice2" which are implemented from same base class i.e. "HeadOffice".

 

Hybrid Inheritance

 

This is a special type of inheritance and can be achieved from any combination of single, hierarchical and multi level inheritance known as hybrid inheritance.

 


 

In the below code example i have combined hierarchical inheritance and multi level inheritance together.

 

class HeadOffice{

 

public void HeadOfficeAddress(){ Console.WriteLine("Head Office Address");

}

}

class BranchOffice1 : HeadOffice{

 

public void BranchOfficeAddress(){ Console.WriteLine("Branch Office Address");

}

}

class BranchOffice2 : HeadOffice{

 

public void BranchOfficeAddress(){ Console.WriteLine("Branch Office Address");

}

 

}

 

Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail
C# and .NET Framework : Inheritance in C# |


Privacy Policy, Terms and Conditions, DMCA Policy and Compliant

Copyright © 2018-2023 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.