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.
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.
Now observe what happens when you call Speak through a base class reference:
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, oroverride. - 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 bepublic.
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.
Now compare the behavior:
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:
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:
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:
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
- What is the difference between using and await using? And how can I decide which one to use?
- What is the difference between using IDisposable vs a destructor in C?
- What is the difference between .Wait vs .GetAwaiter.GetResult?
- What is the difference between X509Certificate2 and X509Certificate in .NET?
- What is the 'dynamic' type in C 4.0 used for?
- What is the equivalent of bigint in C?
- What is the equivalent of the C# 'var' keyword in Java?
- What is the F language created to accomplish?

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack 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.