C# program to demonstrate the example of single inheritance

Single Inheritance in C#: Here, we are going to learn about the single inheritance and its C# implementation.
Submitted by Nidhi, on August 20, 2020

Here we will create a C# program to demonstrate the Single inheritance. Here we will create the Man, and Employee classes to implement single inheritance.

Program:

The source code to demonstrate the single inheritance in C# is given below. The given program is compiled and executed successfully on Microsoft Visual Studio.

//Program to demonstrate the single inheritance in C#.

using System;

class Man
{
    public string name;
    public int age;
    public Man(int age, string name)
    {
        this.name = name;
        this.age = age;
    }
}

class Employee: Man
{
    public int emp_id;
    public int emp_salary;

    public Employee(int id, int salary,string name,int age):base(age,name)
    {
        emp_id = id;
        emp_salary = salary;
    }
    public void Print()
    {
        Console.WriteLine("Emp ID:      " + emp_id      );
        Console.WriteLine("Emp Name:    " + name        );
        Console.WriteLine("Emp Salary:  " + emp_salary  );
        Console.WriteLine("Emp Age:     " + age         );
    }
    static void Main(string[] args)
    {
        Employee emp = new Employee(101, 1000, "Rahul", 31);
        emp.Print();
    }
}

Output:

Emp ID:      101
Emp Name:    Rahul
Emp Salary:  1000
Emp Age:     31
Press any key to continue . . .

Explanation:

In the above program, we created two classes Man, and Employee. Here we inherited Man class into Employee class.  Both classes contain constructors to initialize data members. Here we also created one more method Main() in the Employee class. Here we created the object of Employee class and print the Employee detail on the console screen.






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.