C# Example for Hierarchical Inheritance

CsharpServer Side ProgrammingProgramming

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");
         }
      }
   }
}
raja
Published on 24-Jul-2018 14:41:28
Advertisements