Q:

C# program to demonstrate the example of multi-level inheritance

0

C# program to demonstrate the example of multi-level inheritance

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Program:

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

// program to demonstrate the multi-level inheritance in C#

using System;

class Human
{
    public string name;
    public Human(string na)
    {
        name = na;
    }
}

class Man : Human
{
    public int age;
    public Man(int age, string name):base(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 three classes HumanMan, and Employee. Here we inherited Human class into Man class and then Man class into Employee class.  Every class contains a constructor to initialize data members. Here we also created one more method Main() in the Employee class. Here we created an object of Employee class and print the Employee detail on the console screen.

 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C# Basic Programs | Class, Object, Methods

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
C# program to demonstrate the example of hierarchi... >>
<< C# program to demonstrate the example of single in...