C# Example for Single Inheritance



The following is an example of Single Inheritance in C#. In the example, the base class is Father and declared like the following code snippet −

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

Our derived class is Son and is declared below −

class Son : Father {    public void DisplayOne() {       Console.WriteLine("DisplayOne");    } }

Example

The following is the complete example to implement Single Inheritance in C#.

Live Demo

using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace MyAplication {    class Demo {       static void Main(string[] args) {          // Father class          Father f = new Father();          f.Display();          // Son class          Son s = new Son();          s.Display();          s.DisplayOne();          Console.ReadKey();       }       class Father {          public void Display() {             Console.WriteLine("Display");          }       }       class Son : Father {          public void DisplayOne() {             Console.WriteLine("DisplayOne");          }       }    } }

Output

Display Display DisplayOne
Updated on: 2020-06-19T08:58:59+05:30

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements