What is Inheritance?

What is Inheritance in C#?
Last updated:11/7/2019 12:52:41 AM

1 Answers

Shikhar Arora
Shikhar Arora

Inheritance is the ability to access methods and properties of the base class or parent class in the child class.

namespace Inheritance

{
 // Base class or parent class for Manager class.
    class Employee
    {
        public void EmpTask()
        {
            Console.WriteLine("Task for Employee");
        }
    }
 //child class for Employee class
 //base or parent class for Program class
    class Manager:Employee
    {
        public void ManTask()
        {
            Console.WriteLine("Task for Manager");
        }
    }
 //Child class for Employee and Manager Class
    class Program:Manager
    {

        static void Main(string[] args)
        {
            Program obj = new Program(); //Instantiating Program class
      //calling methods from Employee class and Manager class
            obj.EmpTask();
            obj.ManTask();
            Console.ReadLine();
        }
    }
}

Answer