C#
override keyword
new keyword
programming
object-oriented programming

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

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

In C#, both override and new let a derived class provide its own version of a method that already exists in a base class. However, they behave very differently at runtime. The override keyword enables polymorphism, meaning the derived class version runs even when the object is referenced through the base type. The new keyword hides the base class member, meaning which version runs depends entirely on the type of the reference variable. Understanding this distinction is essential for designing class hierarchies that behave predictably. This article explains both keywords with side-by-side examples so the difference becomes clear.

The override Keyword

The override keyword replaces a virtual or abstract method from a base class with a new implementation in the derived class. The critical point is that this replacement is polymorphic. No matter how the object is referenced, the overridden version runs.

csharp
1public class Animal
2{
3    public virtual string Speak()
4    {
5        return "Some generic sound";
6    }
7}
8
9public class Dog : Animal
10{
11    public override string Speak()
12    {
13        return "Woof!";
14    }
15}

Now observe what happens when you call Speak through a base class reference:

csharp
Animal myPet = new Dog();
Console.WriteLine(myPet.Speak());  // Output: "Woof!"

Even though myPet is declared as type Animal, the runtime sees that the actual object is a Dog and calls the Dog version of Speak. This is polymorphism in action, and it is the primary purpose of override.

Requirements for override

  • The base class method must be marked virtual, abstract, or override.
  • The derived class method must have the same signature (name, parameters, return type).
  • You cannot change the access modifier. If the base method is public, the override must also be public.

The new Keyword

The new keyword hides a base class member rather than overriding it. The hidden member still exists on the base class, and which version runs depends on the type of the variable, not the type of the object.

csharp
1public class Animal
2{
3    public virtual string Speak()
4    {
5        return "Some generic sound";
6    }
7}
8
9public class Cat : Animal
10{
11    public new string Speak()
12    {
13        return "Meow!";
14    }
15}

Now compare the behavior:

csharp
1Cat myCat = new Cat();
2Console.WriteLine(myCat.Speak());  // Output: "Meow!"
3
4Animal myAnimal = new Cat();
5Console.WriteLine(myAnimal.Speak());  // Output: "Some generic sound"

When the variable type is Cat, the Cat version runs. When the variable type is Animal, the Animal version runs, even though the actual object is a Cat. The new keyword breaks the polymorphic chain.

When new Is Optional

If you define a method in a derived class with the same name as a base class method but do not use either override or new, the compiler generates a warning suggesting you add new. The behavior is identical to new, but adding the keyword explicitly tells readers of your code that the hiding is intentional.

Side-by-Side Comparison

This example puts both keywords in the same program to make the difference unmistakable:

csharp
1public class Base
2{
3    public virtual string GetInfo()
4    {
5        return "Base";
6    }
7}
8
9public class OverrideChild : Base
10{
11    public override string GetInfo()
12    {
13        return "OverrideChild";
14    }
15}
16
17public class NewChild : Base
18{
19    public new string GetInfo()
20    {
21        return "NewChild";
22    }
23}
24
25class Program
26{
27    static void Main()
28    {
29        Base obj1 = new OverrideChild();
30        Base obj2 = new NewChild();
31
32        Console.WriteLine(obj1.GetInfo());  // "OverrideChild" (polymorphic)
33        Console.WriteLine(obj2.GetInfo());  // "Base" (hidden, not polymorphic)
34    }
35}

With override, the derived version always wins regardless of the reference type. With new, the base version runs when accessed through a base type reference.

Calling the Base Implementation

With override, you can still access the base class version inside the derived method using the base keyword:

csharp
1public class Dog : Animal
2{
3    public override string Speak()
4    {
5        string baseSound = base.Speak();
6        return $"{baseSound} ... actually, Woof!";
7    }
8}

This is useful when you want to extend the base behavior rather than completely replace it.

With new, calling base.Speak() also works, but it is less common because the design intent of new is typically to provide an entirely separate implementation rather than build on the base.

When to Use Each

Use override when you are designing a class hierarchy where derived classes should be able to customize behavior while maintaining a consistent interface. This is the standard approach for implementing strategy patterns, template method patterns, and any design where code works with base class references.

Use new when a derived class happens to have a method with the same name as a base class method, but the two are not conceptually related. This is rare in well-designed code. One practical scenario is when you inherit from a third-party class and need a method with the same name but different semantics.

In most real-world applications, override is the correct choice. Using new when you intended override is a common source of subtle bugs.

Multi-Level Inheritance

The difference becomes even more important with deeper hierarchies:

csharp
1public class A
2{
3    public virtual string WhoAmI() => "A";
4}
5
6public class B : A
7{
8    public override string WhoAmI() => "B";
9}
10
11public class C : B
12{
13    public new string WhoAmI() => "C";
14}
15
16class Program
17{
18    static void Main()
19    {
20        A obj = new C();
21        Console.WriteLine(obj.WhoAmI());  // "B"
22    }
23}

Class B overrides A's method, so the polymorphic chain follows from A to B. Class C uses new, which breaks the chain. When obj is accessed through type A, the runtime walks the override chain and stops at B, never reaching C's hidden version.

Common Pitfalls

Using new when you meant override. This is the single most common mistake. Your derived class method works fine when called directly, but stops working when the object is passed to code that uses the base type. This often surfaces in collections like List<Animal> where polymorphism is expected.

Forgetting to mark the base method as virtual. You cannot use override unless the base method is virtual or abstract. If you forget virtual, the compiler will suggest new, which changes the behavior from what you intended.

Ignoring compiler warnings. When you hide a base member without new, the compiler warns you. Do not ignore this warning. It is telling you that your code may behave unexpectedly in polymorphic scenarios.

Summary

The override keyword provides polymorphic method dispatch. The derived version runs regardless of the reference type, which is what most object-oriented designs require. The new keyword hides the base member, meaning which version runs depends on the declared type of the variable. In practice, prefer override for intentional polymorphism and reserve new for the rare case where you need a method with the same name that is conceptually unrelated to the base class version. Always pay attention to compiler warnings about member hiding, as they often indicate a missing override keyword.


Related reading
Course
Intermediate
27 lessons
14 hours
OOD Fundamentals

Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions