C# Program For Hierarchical Inheritance
Last Updated :
30 Sep, 2021
Inheritance is a basic aspect of object-oriented programming. A superclass, also known as a base class, is a class whose members are inherited, whereas a subclass, also known as a derived class, is a class that inherits from a superclass. They are also known as the parent and child classes, respectively. In the same way that a child inherits the traits of his or her parents, and parents inherit the characteristics of their predecessors, inheritance in programming languages works in the same way.
Hierarchical Inheritance
It is a way of transmitting features from a parent class to a base, child, or subclass in terms of technical terms and the object-oriented aspect. The parent class or superclass is the class from which the properties are taken, i.e. the features are inherited. Hierarchical inheritance describes a situation in which a parent class is inherited by multiple subclasses. A type of inheritance in which more than one class is inherited from a single parent or base class is known as hierarchical inheritance. The base class shares many of the same properties as the parent class, especially those that are common in the parent class. A single base class gives rise to many classes. It's like having several children, each with their own set of characteristics acquired from their parents. For example, In the diagram below, class A acts as the base class(parent class) for the child classes B, C, and D.

Example 1:
The base class in the following example is Father, and the derived classes are ChildFirst and ChildSecond. We've created objects from both derived classes and are calling the same base class function.
C#
// C# program to illustrate the above concept
using System;
// Base Class
public class Father
{
public string FatherName()
{
return "Ravi";
}
}
// Derived Class
public class ChildFirst : Father
{
public string ChildDName()
{
return "Rohan";
}
}
// Derived Class
public class ChildSecond : Father
{
public string ChildDName()
{
return "Nikhil";
}
}
class GFG{
static public void Main()
{
ChildFirst first = new ChildFirst();
// Displaying Child Name and Father Name for
// ChildFirst
Console.WriteLine("My name is " + first.ChildDName() +
". My father name is " +
first.FatherName() + ".");
ChildSecond second = new ChildSecond();
// Displaying Child Name and Father Name for
// ChildSecond
Console.WriteLine("My name is " + second.ChildDName() +
". My father name is " +
second.FatherName() + ".");
}
}
OutputMy name is Rohan. My father name is Ravi.
My name is Nikhil. My father name is Ravi.
Example 2:
In the following code, we created three classes: Person, Teacher, and Doctor. In this example, the Person class was inherited by both the Teacher and Doctor classes. A constructor in every class is used to initialize data members. Then we created Teacher and Doctor objects and used TeacherDetails() and DoctorDetails() to produce information for the Teacher and Doctor respectively.
C#
// C# program to illustrate the above concept
using System;
// Base Class
class Person
{
public string name;
public int aadhar_id;
public int age;
public Person(int aadhar_id, int age, string name)
{
this.aadhar_id = aadhar_id;
this.name = name;
this.age = age;
}
}
// Derived Class
class Teacher : Person
{
public int teacher_salary;
public Teacher(int aadhar_id, int salary,
string name, int age) : base(aadhar_id,
age, name)
{
teacher_salary = salary;
}
public void TeacherDetails()
{
Console.WriteLine("teacher ID: " + aadhar_id);
Console.WriteLine("teacher Name: " + name);
Console.WriteLine("teacher Salary: " + teacher_salary);
Console.WriteLine("teacher Age: " + age);
}
}
// Derived Class
class Doctor : Person
{
public int doctor_fees;
public Doctor(int aadhar_id, int fees,
string name, int age) : base(aadhar_id,
age, name)
{
doctor_fees = fees;
}
public void DoctorDetails()
{
Console.WriteLine("Doctor ID: " + aadhar_id);
Console.WriteLine("Doctor Name: " + name);
Console.WriteLine("Doctor Fees: " + doctor_fees);
Console.WriteLine("Doctor Age: " + age);
}
}
class GFG{
static public void Main()
{
// Creating objects
Teacher t = new Teacher(25054, 50000, "Sanjay", 28);
Doctor d = new Doctor(25045, 750, "Rohit", 32);
t.TeacherDetails();
Console.WriteLine(
"-----------------------------------");
d.DoctorDetails();
}
}
Outputteacher ID: 25054
teacher Name: Sanjay
teacher Salary: 50000
teacher Age: 28
-----------------------------------
Doctor ID: 25045
Doctor Name: Rohit
Doctor Fees: 750
Doctor Age: 32
Similar Reads
C++ Hierarchical Inheritance
Inheritance is a feature of Object-Oriented-programming in which a derived class (child class) inherits the property (data member and member functions) of the Base class (parent class). For example, a child inherits the traits of their parents. In Hierarchical inheritance, more than one sub-class in
4 min read
Inheritance Hierarchies in DBMS
Inheritance Hierarchies are crucial to building a structured and well-organized database. It is comparable to the idea of inheritance found in object-oriented programming languages. The main ideas of inheritance hierarchies in database management systems (DBMS) will be covered in this article, along
7 min read
Multiple Inheritance in Programming
Multiple Inheritance in programming is like a family tree for your code. You have a bunch of classes, which are like different members of a family. When one class inherits from another, it's like a child inheriting traits from their parents. The child class gets all the abilities (methods) and prope
6 min read
Hierarchical Semantic Networks in AI
Hierarchical semantic networks are a crucial component of knowledge representation in cognitive science, artificial intelligence (AI), and linguistics. They provide a structured way to organize and infer relationships between concepts. By mapping out how ideas and entities are related in a tiered fa
6 min read
Hybrid Inheritance In C++
Before jumping into Hybrid Inheritance, let us first know what is inheritance. Inheritance is a fundamental OOP concept in C++ that allows a new class, also known as a subclass or derived class, to inherit properties and methods from an already-existing class, also known as a superclass or base clas
6 min read
C# | Inheritance in interfaces
C# allows the user to inherit one interface into another interface. When a class implements the inherited interface then it must provide the implementation of all the members that are defined within the interface inheritance chain.Important Points: If a class implements an interface, then it is nece
3 min read
C# | Inheritance in Constructors
Introduction: Inheritance in constructors is a feature in C# that allows a derived class to inherit the constructor of its base class. This means that the derived class can use the constructor of the base class to initialize its own fields and properties. This feature saves a lot of code duplication
8 min read
How to Manage Hierarchical Data in MySQL?
Managing hierarchical data in MySQL poses a unique set of challenges due to the relational nature of traditional database systems. Hierarchical data structures, such as organizational charts or category hierarchies, require thoughtful strategies for storage and retrieval. In this article, we will ex
3 min read
Inheritance in Objective-C
In general, Inheritance is a mechanism by which a subordinate or child class acquires the traits and qualities of a superordinate class or other derived classes. It also allows extra features like taking child class properties and using them in other derived classes. The syntax of classes is used to
4 min read
C++ Inheritance Access
Prerequisites:Class-Object in C++Inheritance in C++Before learning about Inheritance Access we need to know about access specifiers. There are three Access specifiers in C++. These are:public - members are accessible from outside the class, and members can be accessed from anywhere.private - members
4 min read