What is inheritance?

What is inheritance?

Last updated:10/14/2019 2:30:48 AM

1 Answers

Om ji mishra
Om ji mishra

Inheritance is an important pillar of oops. By using this property in C# we can use all features from the base class in any other classes. We can reuse the code by using inheritance.

Some important things about inheritance: -

  • When we have to do inherit any class then we use " : " (colon)
  • Syntax: - class <derived class name> : <base class name>
  • We cannot inherit the private members, function, methods, etc.
  • The class whose features are inherited is known as a super class (or a base class or a parent class)
  • The class that inherits the other class is known as sub class (or a derived class, extended class, or child class)
Code: -

using System;
   public class Employee
    {
       public float salary = 40000;
   }
   public class Programmer: Employee
   {
       public float bonus = 10000;
   }
   class TestInheritance{
       public static void Main(string[] args)
        {
            Programmer p1 = new Programmer();
            float Total_Salary= p1.salary+p1.bonus;

            Console.WriteLine("Total Salary " + Total_Salary);

        }
    }
Code: -

using System;
namespace om
{

   public class Employee
    {
       public float salary = 40000;
   }
   public class bonusEmployee: Employee
   {
       public float bonus = 10000;
       public void Get_total()
       {
         float Total= base.salary+bonus;
         Console.WriteLine("Toatal salary {0}", Total);
       }
   }
   class TestInheritance{
       public static void Main(string[] args)
        {
          bonusEmployee p1 = new bonusEmployee();
          p1.Get_total();
        }
    }
}

Answer