In hierarchical inheritance, multiple classes are inherited from the base class.
In the example, our base class is Father -
class Father { public void display() { Console.WriteLine("Display..."); } }
which has Son and Daughter as derived classes . Let us how to add derived class in inheritance -
class Son : Father { public void displayOne() { Console.WriteLine("Display One"); } }
Here is the complete example to implement 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"); } } } }
The above is the detailed content of C# example of hierarchical inheritance. For more information, please follow other related articles on the PHP Chinese website!