override (C# Reference) 

The override modifier is required to extend or modify the abstract or virtual implementation of an inherited method, property, indexer, or event.

In this example, the class Square must provide an overridden implementation of Area because Area is inherited from the abstract ShapesClass:

abstract class ShapesClass
{
    abstract public int Area();
}

class Square : ShapesClass
{
    int x, y;
    // Because ShapesClass.Area is abstract, failing to override
    // the Area method would result in a compilation error.
    public override int Area()
    {
        return x * y;
    }
}

For more information on using the override keyword, see Versioning with the Override and New Keywords and Knowing when to use Override and New Keywords.

Remarks

An override method provides a new implementation of a member inherited from a base class. The method 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. For information on inheritance, see Inheritance.

You cannot override a non-virtual or static method. The overridden base method must be virtual, abstract, or override.

An override declaration cannot change the accessibility of the virtual method. Both the override method and the virtual method must have the same access level modifier.

You cannot use the modifiers new, static, virtual, or abstract to modify an override method.

An overriding property declaration must specify the exact same access modifier, type, and name as the inherited property, and the overridden property must be virtual, abstract, or override.

Example

This example defines a base class called Employee, and a derived class called SalesEmployee. The SalesEmployee class includes an extra property, salesbonus, and overrides the method CalculatePay in order to take it into account

using System;
class TestOverride
{
    public class Employee
    {
        public string name;

        // Basepay is defined as protected, so that it may be 
        // accessed only by this class and derrived classes.
        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;
        }
    }

    // Derive a new class from Employee.
    public class SalesEmployee : Employee
    {
        // New field that will affect the base pay.
        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;
        }
    }

    static void Main()
    {
        // Create some new employees.
        SalesEmployee employee1 = new SalesEmployee("Alice", 
                      1000, 500);
        Employee employee2 = new Employee("Bob", 1200);

        Console.WriteLine("Employee " + employee1.name + 
                  " earned: " + employee1.CalculatePay());
        Console.WriteLine("Employee " + employee2.name + 
                  " earned: " + employee2.CalculatePay());
    }
}

Output

Employee Alice earned: 1500
Employee Bob earned: 1200

C# Language Specification

For more information, see the following sections in the C# Language Specification:

  • 1.6.5.4 Virtual, override, and abstract methods

  • 10.5.4 Override methods

See Also

Reference

Inheritance (C# Programming Guide)
C# Keywords
Modifiers (C# Reference)
abstract (C# Reference)
virtual (C# Reference)
new (C# Reference)

Concepts

C# Programming Guide
Polymorphism (C# Programming Guide)

Other Resources

C# Reference