What is the difference between the override and new keywords in C#?

C#.NetOop

C# Problem Overview


What is the difference between the override and new keywords in C# when defining methods in class hierarchies?

C# Solutions


Solution 1 - C#

The following page summarizes your question very nicely.

Knowing When to Use Override and New Keywords

Summary

Override: When a method of a base class is overridden in a derived class, the version in the derived class is used, even if the calling code didn't "know" that the object was an instance of the derived class.

New: If you use the new keyword instead of override, the method in the derived class doesn't override the method in the base class, it merely hides it.

If you don't specify either new or overrides, the resulting output is the same as if you specified new, but you'll also get a compiler warning (as you may not be aware that you're hiding a method in the base class method, or indeed you may have wanted to override it, and merely forgot to include the keyword).

Override: used with virtual/abstract/override type of method in base class

New: when base class has not declared method as virtual/abstract/override

Solution 2 - C#

new will shadow the method with a completely new method (which may or may not have the same signature) instead of overriding it (in which case the new method must have the same signature), meaning that polymorphism won't work. For example, you have these classes:

class A {
    public virtual int Hello() {
        return 1;
    }
}

class B : A {
    new public int Hello(object newParam) {
        return 2;
    }
}

class C : A {
    public override int Hello() {
        return 3;
    }
}

If you do this:

A objectA;
B objectB = new B();
C objectC = new C();

Console.WriteLine(objectB.Hello(null)); // 2
Console.WriteLine(objectC.Hello()); // 3

objectA = objectB;

Console.WriteLine(objectA.Hello()); // 1

objectA = objectC;

Console.WriteLine(objectA.Hello()); // 3

Since you can define new method signatures with new, it's impossible for the compiler to know that the instance of A is actually an instance of B and the new method B defines should be available. new can be used when the parent object's method, property, field or event is not declared with virtual, and because of the lack of virtual the compiler won't “look up” the inherited method. With virtual and override, however, it works.

I would strongly recommend you avoid new; at best, it’s confusing, because you’re defining a method with a name that could be recognized as something else, and at worst, it can hide mistakes, introduce seemingly impossible bugs, and make extending functionality difficult.

Solution 3 - C#

Looks like an old question, let me try a different answer:

  1. new : as the name says, it is a new member in the family of inheritance hierarchy and this will be used as base member for further down the chain (if marked as virtual).

  2. override : It means I don't accept my parent class' member implementation and I will do differently.

Solution 4 - C#

Consider the following class hierarchy:

using System;

namespace ConsoleApp
{     
     public static class Program
     {   
          public static void Main(string[] args)
          {    
               Overrider overrider = new Overrider();
               Base base1 = overrider;
               overrider.Foo();
               base1.Foo();
        
               Hider hider = new Hider();
               Base base2 = hider;
               hider.Foo();
               base2.Foo();
          }   
     }   

     public class Base
     {
         public virtual void Foo()
         {
             Console.WriteLine("Base      => Foo");
         }
     }

     public class Overrider : Base
     {
         public override void Foo()
         {
             Console.WriteLine("Overrider => Foo");
         }
     }

     public class Hider : Base
     {
         public new void Foo()
         {
             Console.WriteLine("Hider     => Foo");
         }
     }
}    

Output of above codes must be:

Overrider => Foo
Overrider => Foo

Hider     => Foo
Base      => Foo

> * A subclass overrides a virtual method by applying the override modifier: > * If you want to hide a member deliberately, in which case you can apply the new modifier to the member in the subclass. The new modifier does nothing more than suppress the compiler warning that would otherwise result

Solution 5 - C#

override lets you override a virtual method in a base class so that you can put a different implementation in. new will hide a non-virtual method in a base class.

Solution 6 - C#

The simple difference is that override means the method is virtual (it goes in conduction with virtual keyword in base class) and new simply means it's not virtual, it's a regular override.

So both really are function overrides, one is with virtual characteristics, the other not.

What does mean exactly? It simply means polymorphism will not be in play for `new' methods.

The following image illustration might make this clear.

enter image description here

Note if you don't use new keyword, it is still implied but it will generate a warning message.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionjaywaycoView Question on Stackoverflow
Solution 1 - C#NahydrinView Answer on Stackoverflow
Solution 2 - C#Ry-View Answer on Stackoverflow
Solution 3 - C#DhananjayView Answer on Stackoverflow
Solution 4 - C#Sina LotfiView Answer on Stackoverflow
Solution 5 - C#Daniel MannView Answer on Stackoverflow
Solution 6 - C#zarView Answer on Stackoverflow