Q:

C# program to demonstrate the example of multilevel inheritance with method overriding

0

C# program to demonstrate the example of multilevel inheritance with method overriding

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 with method overriding in C# is given below. The given program is compiled and executed successfully on Microsoft Visual Studio.

//Program to demonstrate the multilevel inheritance 
//with the virtual method in C#.

using System;

class Human
{
    public string name;
    public Human(string na)
    {
        name = na;
    }
    public virtual void printInfo()
    {
        Console.WriteLine("Name: " + name);
    }
}

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

    public override void printInfo()
    {
        base.printInfo();
        Console.WriteLine("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 override void printInfo()
    {
        Console.WriteLine("Emp ID:      " + emp_id);
        base.printInfo();
        Console.WriteLine("Emp Salary:  " + emp_salary);   
    }
    
    static void Main(string[] args)
    {
        Employee emp = new Employee(101, 1000, "Rahul", 31);
        emp.printInfo();
    }
}

Output:

Emp ID:      101
Name: Rahul
Age: 31
Emp Salary:  1000
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 and printInfo() method. Here we override printInfo() method in Man and Employee class. 

The Employee class also contain the Main() method. In the Main() method we created object emp of Employee class and call printInfo() method that will print.

 

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 simple interface... >>
<< C# program to demonstrate the example of hierarchi...