SoFunction
Updated on 2025-03-07

A detailed explanation of the difference between override and override in C#

1. Definition of rewrite and overwrite

1.1 Definition of override

In C#, use the override keyword to override a virtual or abstract method in a parent class. The override keyword is used to indicate the compiler. I want to use a method from the derived class to override the method of the same name in the base class. Polymorphism can be achieved by rewriting methods in the base class. Detailed examples of rewriting and virtual methods and abstract methods, step-by-stepC# virtual method and abstract method examples

The syntax of the override keyword is as follows:

public override returnType MethodName(ParameterList)
{
    // Method implementation}

Among them, public represents access modifier; override represents overwrite virtual methods or abstract methods; returnType represents the return type of the method; MethodName represents the method name; ParameterList represents the parameter list of the method.

It should be noted that the access modifier of the overridden method must be the same as or looser as the access modifier of the overridden method.

1.2 Definition of overrides (new)

In C#, use the new keyword to override a member method or member variable in a parent class. The new keyword is used to indicate that a method or variable in the derived class will hide the method or variable of the same name in the base class, thereby changing the original layout relationship in the inheritance system.

The syntax of the new keyword is as follows:

new returnType MemberName(ParameterList)
{
    // Member implementation}

Where, returnType represents the return type of the member; MemberName represents the name of the member; ParameterList represents the parameter list of the member.

2. Differences and examples of the two

Here is an example:

//Premium class  public class FatherClass
  {
    public virtual string GetName()
    {
      return "I'm a virtual method in the parent class";
    }
  }
  //Subclass 1  public class SonClass1 : FatherClass
  {
    public override string GetName()
    {
      return "I'm the rewrite method in subclass 1";
    }
  }
  //Subclass 2  public class SonClass2 : FatherClass
  {
    public new string GetName()
    {
      return "I'm the overlay method in subclass 2";
    }
  }

Then call it through the program entry function:

 public class Program
  {
    public static void Main(string[] args)
    {
      FatherClass fatherClass = new FatherClass();
      ("Directly call the virtual method in the parent class:");
      (());

      SonClass1 s1 = new SonClass1();
      ("Directly call the override method in subclass 1:");
      (());

      SonClass2 s2 = new SonClass2();
      ("Directly call the override method in subclass 2:");
      (());

      FatherClass f1 = new SonClass1();
      ("Declare the parent class, call the override method in child class 1:");
      (());

      FatherClass f2 = new SonClass2();
      ("Declare the parent class, call the override method in child class 2:");
      (());
    }
  }

When running the program, the result is as follows:

Directly call virtual methods in the parent class: I am the virtual methods in the parent class
Directly call the rewrite method in subclass 1: I am the rewrite method in subclass 1
Directly call the overlay method in subclass 2: I am the overlay method in subclass 2

Declare the parent class and call the override method in subclass 1: I am the override method in subclass 1
Declare the parent class, call the override method in child class 2: I am a virtual method in parent class

Summarize

1: No matter whether it is rewriting or overwriting, it will not affect the functions of the parent class itself.

2: When creating a parent class with a subclass, such as FatherClass f1 = new SonClass1(), rewriting will change the function of the parent class, that is, calling the function of the child class; if overwriting does not, the function of the parent class will still be called.

3: Virtual methods and real methods can be overwritten (new), but abstract methods and interfaces cannot.

4: Abstract methods, interfaces, methods marked as virtual can be override, but real methods cannot.

5: The frequency of rewriting is relatively high, and it realizes polymorphism; the frequency of overwriting is relatively low, and it is used when inheriting classes that cannot be modified before.

Small expansion: When overwriting a member method or variable with the new keyword, the position of the hidden member in the class hierarchy changes, and it is redefined in the derived class rather than inherited from the base class. When a base class object refers to a derived class object, the hidden members in the derived class are not included by the base class reference, so they are inaccessible. In derived classes, the newly defined members will work and have nothing to do with the same name member in the base class.

This is the end of this article about a detailed explanation of the difference between override and override in C#. For more related C# rewriting, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!