C# Example for Hierarchical Inheritance



More than one class is inherited from the base class in Hierarchical Inheritance.

In the example, our base class is Father

class Father {    public void display() {       Console.WriteLine("Display...");    } }

It has Son and Daughter as the derived class. Let us how to add a derived class in Inheritance −

class Son : Father {    public void displayOne() {       Console.WriteLine("Display One");    } }

Example

The following the complete example of implementing Hierarchical Inheritance in C# −

using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Inheritance {    class Test {       static void Main(string[] args) {          Father f = new Father();          f.display();          Son s = new Son();          s.display();          s.displayOne();          Daughter d = new Daughter();          d.displayTwo();          Console.ReadKey();       }       class Father {          public void display() {             Console.WriteLine("Display...");          }       }       class Son : Father {          public void displayOne() {             Console.WriteLine("Display One");          }       }       class Daughter : Father {          public void displayTwo() {             Console.WriteLine("Display Two");          }       }    } }
Updated on: 2020-06-19T08:32:41+05:30

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements