articles

Home / DeveloperSection / Articles / Method overriding in C#

Method overriding in C#

Anonymous User3857 05-May-2016

Method overriding is a feature that allows you to invoke functions (that have same signatures) that belong to different classes in the same hierarchy of inheritance using the base class reference. C# makes use of two keywords: ‘virtual’ and ‘overrides' to accomplish Method overriding.


An override method provides a new implementation of a member that is inherited from a base class. The method that is overridden by an override declaration is known as the overridden base method. The overridden base method must have the same signature as the override method 

Let's understand this through small examples:


Employee class

Create  Employee class
 
using System;
using System.Collections.Generic;     
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace OoopsConcepts
{ 
    publicclassEmployee
    { 
        publicstring name; 
        protecteddecimal basepay;
 
        // Constructor to set the name and basepay values.
        public Employee(string name, decimal basepay)
        {
            this.name = name;
            this.basepay = basepay;
        }
 
        // Declared virtual so it can be overridden.
        publicvirtualdecimal CalculatePay()
        {
            return basepay;
        } 
    }
}


and after create another class 'SalesEmployee' that inherits to 'Employee',


SalesEmployee class

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace OoopsConcepts
{
    publicclassSalesEmployee : Employee
    {
        privatedecimal salesbonus;
 
        // The constructor calls the base-class version, and initializes the salesbonus field.
        public SalesEmployee(string name, decimal basepay, decimal salesbonus): base(name, basepay)
        {
            this.salesbonus = salesbonus;
        }
 
        // Override the CalculatePay method to take bonus into account.
        publicoverridedecimal CalculatePay()
        {
            return basepay + salesbonus;
        }
 
    }
}


OverridingTest class

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace OoopsConcepts
{                                                      
    classOverridingTest
    {
        staticvoid Main(string[]args)
        { 
         // Method Override
            SalesEmployee employee1 = newSalesEmployee("Vinay", 800, 500);
            Employee employee2 = newEmployee("Subhash", 1800);
            Console.WriteLine("Employee of Mindstick- " + employee1.name + " earned: " + employee1.CalculatePay());
            Console.WriteLine("Employee of Mindstick- " + employee2.name + " earned: " + employee2.CalculatePay());
      } 
    } 
}


OUTPUT: Employee of MindStick- Vinay earned: 1500

           Employee of MindStick- Subhash earned: 1200


Updated 27-Mar-2018
I am a content writter !

Leave Comment

Comments

Liked By