---
title: "Method overriding in C#"  
description: "Method overriding is a feature that allows you to invoke functions (that have same signatures) that belong to different classes in the same hierarchy"  
author: "Anonymous User"  
published: 2016-05-05  
updated: 2018-03-27  
canonical: https://www.mindstick.com/articles/12034/method-overriding-in-c-sharp  
category: "c#"  
tags: ["c#", "oops"]  
reading_time: 2 minutes  

---

# Method overriding in C#

[Method overriding](https://www.mindstick.com/articles/12227/method-overloadind-and-method-overriding-in-c-sharp) is a feature that allows you to invoke [functions](https://www.mindstick.com/forum/34086/jquery-callback-functions) (that have same signatures) that belong to different classes in the same hierarchy of [inheritance](https://www.mindstick.com/articles/80/inheritance-in-c-sharp) using the [base class](https://www.mindstick.com/blog/116/base-class-initilization) reference. C# makes use of two [keywords](https://www.mindstick.com/articles/12328/how-to-research-for-keywords): ‘virtual’ and ‘overrides' to accomplish Method overriding.

\
An **override** method provides a new [implementation](https://answers.mindstick.com/qa/44913/who-is-the-minister-of-statistics-and-programme-implementation) of a member that is inherited from a base class. The method that is overridden by an override declaration is [known as](https://answers.mindstick.com/qa/38482/name-the-world-s-lightest-material-which-was-manufactured-by-isro-scientists-at-the-vikram-sarabhai-space-centre-thiruvananthapuram-and-is-also-known-as-blue-air) the overridden base method. The overridden base method must have the same [signature](https://www.mindstick.com/interview/516/explain-about-xml-signature) as the override method

**Let's understand this through small examples:**

**\**

**[Employee](https://www.mindstick.com/articles/178134/how-to-write-the-best-welcome-letter-for-new-employees) 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](https://yourviews.mindstick.com/story/1230/top-beneficial-service-pages-of-mindstick-company)- Vinay earned: 1500

Employee of MindStick- Subhash earned: 1200

---

Original Source: https://www.mindstick.com/articles/12034/method-overriding-in-c-sharp

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
