C# Program For Hierarchical Inheritance
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.
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.
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.
My name is Rohan. My father name is Ravi. My name is Nikhil. My father name is Ravi.
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.
teacher ID: 25054 teacher Name: Sanjay teacher Salary: 50000 teacher Age: 28 ----------------------------------- Doctor ID: 25045 Doctor Name: Rohit Doctor Fees: 750 Doctor Age: 32