Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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#.
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
Advertisements