Home > Backend Development > C#.Net Tutorial > C# example of hierarchical inheritance

C# example of hierarchical inheritance

王林
Release: 2023-08-29 18:33:02
forward
654 people have browsed it

层次继承的 C# 示例

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...");
   }
}
Copy after login

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");
   }
}
Copy after login

Example

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");
         }
      }
   }
}
Copy after login

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template