Understanding virtual, override and new keyword in C#
Learn the differences between the virtual, override, and new keywords in C# and how they control method behavior in inheritance hierarchies.
The virtual Keyword
Marking a method as virtual in a base class allows derived classes to provide their own implementation of that method, enabling polymorphism where the correct version is chosen at runtime based on the object's actual type.
The override Keyword
A derived class uses the override keyword to replace the implementation of a virtual (or abstract) method inherited from its base class, ensuring the new behavior is used whenever the method is called through a base reference.
The new Keyword for Method Hiding
When a derived class uses new instead of override, it hides the base class method rather than overriding it. This means the method called depends on the type of the reference used, not the actual object type, which can lead to unexpected behavior if misunderstood.
Choosing the Right Approach
In general, override should be used when you want true polymorphic behavior, while new should be reserved for rare cases where you intentionally want to break the inheritance chain for a specific method.
A Side-by-Side Output Comparison
The clearest way to feel the difference is to call the same method through a base reference and watch which version runs.
class Animal {
public virtual void Speak() => Console.WriteLine("Animal");
}
class Dog : Animal {
public override void Speak() => Console.WriteLine("Dog"); // override
}
class Cat : Animal {
public new void Speak() => Console.WriteLine("Cat"); // new, hides
}
Animal a = new Dog();
a.Speak(); // prints "Dog" -- override follows the object
Animal b = new Cat();
b.Speak(); // prints "Animal" -- new follows the reference type
((Cat)b).Speak(); // prints "Cat" -- only when called as Cat
With override, the real object decides. With new, the declared type of the variable decides. That single line of output is the whole lesson.
What Happens Behind the Scenes
A virtual method gets an entry in the type's method table, and override replaces that entry for the derived type. When you call through any reference, the runtime looks up the entry on the actual object, so the derived version wins. A new method does not touch that table at all — it introduces a separate method that simply shadows the name, so the compiler picks which one to call based purely on the compile-time type.
Picking Between Them in Practice
- Use
virtualplusoverridewhenever you want substitutable behaviour — the normal case in well-designed class hierarchies. - Reach for
newonly when you cannot change the base class and must reuse a name with a different meaning; it is rare and surprising. - Always keep the
overridekeyword rather than relying onnewby accident — the compiler warns you when a hidden method is unintentional, and that warning is worth fixing. - If a method should never be overridden further, mark it
sealed overrideto stop the chain cleanly.
A related keyword worth knowing is abstract. An abstract method is like a virtual one with no body — the base class declares it but forces every concrete derived class to supply an override. Reach for abstract when there is no sensible default behaviour, and for virtual when the base can offer a reasonable default that children may replace. In both cases the call is resolved on the real object, which is exactly the polymorphism that new gives up.
A subtle trap with new appears in collections. If you store several objects as the base type and loop over them calling the method, a new method is skipped in favour of the base version, because the loop variable is typed as the base. An override method runs correctly in that same loop. So method hiding does not just affect single variables — it quietly changes how whole batches of objects behave when handled through a base-type reference.
Takeaway: override keeps polymorphism honest, while new breaks it on purpose. Because new makes behaviour depend on how a variable is typed rather than what it holds, treat it as the exception you justify, not the default you reach for.