Home > DeveloperSection > Beginner > Method overriding in C#

Method overriding in C#


C# C#  OOPS 
Ratings:
0 Comment(s)
 590  View(s)
Rate this:

Method overriding in C#

 


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
{ 
    public class Employee
    { 
        public string name; 
        protected decimal 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.
        public virtual decimal 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
{
    public class SalesEmployee : Employee
    {
        private decimal 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.
        public override decimal CalculatePay()
        {
            return basepay + salesbonus;
        }
 
    }
}


OverridingTest class

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace OoopsConcepts
{                                                      
    class OverridingTest
    {
        static void Main(string[]args)
        { 
         // Method Override
            SalesEmployee employee1 = new SalesEmployee("Vinay", 800, 500);
            Employee employee2 = new Employee("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


Don't want to miss updates? Please click the below button!

Follow MindStick