C# Program to Demonstrate Single Inheritance
Posted by Superadmin on August 13 2022 11:04:43

C# Program to Demonstrate Single Inheritance

 

 

This is a C# Program to illustrate single inheritance.

Problem Description

This C# Program Illustrates Single Inheritance.

Problem Solution

Here In single inheritance we have single base class that is inherited by the derived class and the derived class has all the features of the base class and can add some new features and italso depends on the access specifier that is used at the time of base class inheritance.

Program/Source Code

Here is source code of the C# Program to Caluculate the power exponent value. The C# program is successfully compiled and executed with Microsoft Visual Studio. The program output is also shown below.

/*
 *  C# Program to Illustrate Single Inheritance
 */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Inheritance
{
    class Program
    {
        static void Main(string[] args)
        {
            Teacher d = new Teacher();
            d.Teach();
            Student s = new Student();
            s.Learn();
            s.Teach();
            Console.ReadKey();
        }
        class Teacher
        {
            public void Teach()
            {
                Console.WriteLine("Teach");
            }
        }
        class Student : Teacher
        {
            public void Learn()
            {
                Console.WriteLine("Learn");
            }
        }
    }
}
Program Explanation

This C# program is used to illustrate single inheritance. Here in single inheritance we have single base class that is inherited by the derived class. And the derived class has all the features of the base class and can add some new features and it also depends on the access specifier that is used at the time of base class inheritance.

 

In this program, we have created Teacher and Student class. Create the object variable for teacher class as‘d’ and for student class as‘s’. Then we are calling the teach() and learn() along with the object variable.

Runtime Test Cases
 
Teach
Learn
Teach