This article introduces the concept of inheritance in C# programming with real-time examples. Abstraction, Encapsulation, Inheritance, and Polymorphism are the four pillars of object-oriented programming. Inheritance is one of the features which is used to inherit/acquire the properties and behavior of one class into another class. Thus it enables the reusability of code, makes the code more meaningful and leads to better program structure. Please read the article for concepts of objects and classes if you have not already done so, before this one.
Inheritance is one of the primary characteristics of object-oriented programming. A class whose members are inherited is called a superclass or base class, and the class that inherits from a superclass is called a subclass or derived class. Respectively, we can refer to them as parent class and child class as well. For example, a child inherits the traits of his/her parents, and parents inherit the traits of their grandparents, inheritance in programming languages also work on the similar lines.
This is the basic syntax of creating a derived/child class:
<access modifier> class <base class>
{
...
}
<access modifier> class <derived class> : <base class>
{
...
}
Types of Inheritance in C#
The C# and .NET support only single inheritance. However, we have categorized it into four types as inheritance is transitive, which allows us to define an inheritance hierarchy for a set of types. Also, it does not support multiple inheritance, but it can be achieved using Interface (we will cover the concept of Interface in another article).
Note: More information about how inheritance is transitive, is available at the end of this article.
-
Single Inheritance
-
Hierarchical Inheritance
-
Multilevel Inheritance
-
Multiple Inheritance (achievable using the Interface)
1. Single Inheritance
In this type of inheritance, the derived class inherits properties and behavior from a single base class. It's like a child inherits the traits of his/her parents.
Let's take the example of single inheritance.
Filename: Program.cs
using System;
namespace Studytonight
{
public class Parent
{
public void DisplayParentsAB()
{
Console.WriteLine("A and B are my parents");
}
}
public class Son: Parent
{
public void DisplaySonC()
{
Console.WriteLine("I am the son C");
}
}
public class Program
{
public static void Main(string[] args)
{
Son s = new Son();
s.DisplaySonC();
s.DisplayParentsAB();
}
}
}
Output:
I am the son C
A and B are my parents
In the code above, Parent is the base class, and Son is the derived class. The Son class is inheriting the method of Parent class, and for that, we have created the object of the Son class using the new
keyword.
2. Hierarchical Inheritance
It is a technique for transferring features from a parent class to a base, child, or subclass using object-oriented terminology. The parent class or superclass is the class from which the inherited properties are taken, or inherited features. Hierarchical inheritance describes the situation in which multiple subclasses inherit from a parent class.
Hierarchical inheritance is a type of inheritance in which multiple classes inherit from a single parent or base class. The base class shares many of the same properties as the parent class, particularly the common properties. A single base class generates multiple derived classes. It is comparable to having multiple children, each with a unique set of characteristics inherited from their parents.
Filename: Program.cs
using System;
namespace Studytonight
{
public class Parent
{
public void DisplayParentsAB()
{
Console.WriteLine("A and B are my parents");
}
}
public class ChildC: Parent
{
public void DisplayChildC()
{
Console.WriteLine("I am the child C");
}
}
public class ChildD: Parent
{
public void DisplayChildD()
{
Console.WriteLine("I am the child D");
}
}
public class Program
{
public static void Main(string[] args)
{
ChildC cc = new ChildC();
ChildD cd = new ChildD();
cc.DisplayChildC();
cc.DisplayParentsAB(); // accessing parent class
cd.DisplayChildD();
cd.DisplayParentsAB(); // accessing parent class
}
}
}
Output:
I am the child C
A and B are my parents
I am the child D
A and B are my parents
In the above example, Parent is the base class, and ChildC and ChildD are the derived classes. We have created the objects of both derived classes and are accessing the same base class method. Hence if one base class is derived/inherited by many child classes, it is known as hierarchical inheritance.
3. Multilevel Inheritance
In this type of inheritance, a class inherits another derived/child class which in turn inherits another class. It's like a child inherits the traits of his/her parents, and parents inherit the traits of their grandparents.
Filename: Program.cs
using System;
namespace Studytonight
{
public class Grandparent
{
public Grandparent()
{
Console.WriteLine("Constructor called at run-time");
}
public void DisplayGrandParentsAB()
{
Console.WriteLine("A and B are my grandparents");
}
}
public class Parents: Grandparent
{
public void DisplayParentsCD()
{
Console.WriteLine("C and D are my parents");
}
}
public class Child: Parent
{
public void DisplayChildZ()
{
Console.WriteLine("I am the child Z");
}
}
public class Program
{
public static void Main(string[] args)
{
child cd = new Child();
cd.DisplayChildZ();
cd.DisplayParentsCD();
cd.DisplayGrandParentsAB();
}
}
}
Output:
Constructor called at run-time
I am the son Z
C and D are my parents
A and B are my grandparents
In the above code example, Grandparent is the base class and Parent and Child are the derived classes. The difference here is that the Child class is inheriting the Parent class and Parent class is inheriting the Grandparent class means, one derived class is inheriting from another derived class. Hence, it is called Multilevel Inheritance. The constructor gets called at run-time as the objects get created at run-time.
How Inheritance is Transitive?
Mathematically, transitive means, if a = b and b = c, then automatically a = c, and the same rule follows in programming. Let's take our previous example in multilevel inheritance, if Child class is derived from Parent class and Parent class is derived from Grandparent class, then the Child class can inherit the members, and methods declared in both Grandparent and Parent class.
Transitive: Child -> Parent, Parent -> Grandparent, then Child -> Grandparent
We hope this article helped you to understand the concept of Inheritance in C# language.
Conclusion
Inheritance is a basic idea in object-oriented programming that allows writers to reuse code while creating more efficient and ordered programs. C# supports three kinds of inheritance: solitary, hierarchical, and multilevel, each with its own set of benefits and applications. A class can only derive from one parent class, whereas a class can acquire from numerous classes through hierarchical inheritance. Multilevel inheritance entails building a class structure in which each derived class gets from a parent class. Understanding the various kinds of inheritance in C# can assist writers in writing more effective and stable code for their apps.
Frequently Asked Questions
1. What is single inheritance in C#?
Single inheritance is a mechanism in C# where a class inherits the properties and methods of a single base class. The derived class inherits all the base class members and can add new members to its own class.
2. What is hierarchical inheritance in C#?
Hierarchical inheritance is a mechanism in C# where a base class is inherited by multiple derived classes. This means various classes share the same base class and can inherit the same properties and methods.
3. What is multilevel inheritance in C#?
Multilevel inheritance is a mechanism in C# where the base class is inherited from the derived class, inheriting from another base class. This creates a chain of inheritance, where each class can access the members of the classes above it in the hierarchy.
4. What are the benefits of using inheritance in C#?
Inheritance allows for code reuse, reduces code duplication, and helps create modular and extensible code. It also enables the creation of a hierarchical structure of classes, allowing for easy maintenance and organisation of code.
You may also like: