C# virtual, override and new keyword

Search for a command to run...

No comments yet. Be the first to comment.
In this tutorial, we would learn about static keyword in c# programming language. static : something which belongs to the type itself, not with an instance of the type. The modifier in C# language applies to the following : Variable Methods Class ...
ASP.NET Core Configuration Guide Overview ASP.NET Core uses a layered configuration system where later sources override earlier ones for the same key. Configuration Priority Order Priority Source
Legacy Delphi applications often struggle with outdated UI/UX, performance issues, and monolithic architectures that make refactoring difficult. However, a full rewrite is not always feasible due to time, cost, and the risk of disrupting critical bus...
Entity Framework Core (EF Core) is a powerful and flexible object-relational mapping (ORM) framework for .NET applications, enabling developers to work with relational databases using strongly-typed .NET objects. It simplifies data access by abstract...

In this article we are going to cover some advance scenarios where Options pattern is very useful. We have covered the basic understanding of Options pattern into Options pattern in ASP.NET Core: Introduction article. Please go through it if not fami...

In this article we will learn about Options pattern in ASP.NET Core for dealing with application configurations. The Options pattern uses the C# classes to access and manage the configurations thus providing more flexibility and type safe approach to...

In this article, we would learn about virtual, override and new keyword.
Before moving further to understand about virtual, override and new , it's important to understand how polymorphism is achieved in c#.
The word polymorphism means having "many forms". In OOPs terminology, it has two aspects :
objects of the derived class may be treated as objects of the base class at run time i.e. derived class behavior could be accessed using the base class object.
the base class may define a method that could be overridden by child classes.
in C#, polymorphism is achieved in 3 ways: method overloading, method overriding and method hiding
Now let's discuss how virtual , override and new keyword comes into play to implement polymorphism.
The base class declares a member as virtual. The member could be a method, property or event.
Derived class may override the base class method to provide a new implementation of the base class method into the derived class.
A derived class may hide the base class method using a new keyword.
Enough theory, now we would dive into code to understand these concepts.
public class Animal {
public string Name;
public virtual string AnimalType { get => "base"; }
public virtual string Speak() {
return "Speaks";
}
}
public class Horse : Animal {
public override string AnimalType { get => "Pet"; }
public override string Speak() {
return "Neighs";
}
}
public class Lion: Animal {
public override string AnimalType { get => "Wild"; }
public override string Speak() {
return "Roars";
}
}
public static void Main(string[] args) {
Console.WriteLine("Start Of the Program");
Lion l1 = new Lion();
Animal a = l1;
Console.WriteLine($"This animal is a {a.AnimalType} animal and it {a.Speak()}");
Horse h1 = new Horse();
Animal b = h1;
Console.WriteLine($"This animal is a {b.AnimalType} animal and it {b.Speak()}");
Console.ReadKey();
}
In the above code block, Animal is base class that has few virtual members. These virtual members are being overridden by derived classes Lion and Horse differently
derived classes may hide the base class method i.e. derived class could have a method same ( same method name, return type and parameters) as a base class. new keyword is used to hide the base class method
public class BaseClass {
public virtual void Display() {
Console.WriteLine("BaseClass.Display");
}
}
public class Overrider : BaseClass {
public override void Display() {
Console.WriteLine("Overrider.Display");
}
}
public class Hider : BaseClass {
public new void Display() {
Console.WriteLine("Hider.Foo");
}
}
The difference in the output of the above two classes would be as :
public static void Main(string[] args) {
Console.WriteLine("Start Of the Program");
Overrider over = new Overrider();
BaseClass b1 = over;
over.Display(); // Overrider.Foo
b1.Display(); // Overrider.Foo
Hider h = new Hider();
BaseClass b2 = h;
h.Display(); // Hider.Foo
b2.Display(); // BaseClass.Foo
Console.ReadKey();
}
NOTE: base class method could be hidden even without using the new keyword. new keyword just suppresses the compiler warning which generates without using new.
A method declared as virtual in the base class will be available for an override at each level of the inheritance chain.
public class A
{
public virtual void DoSomething() { }
}
public class B : A
{
public void DoSomethingElse() {}
}
public class C : B
{
public override void DoSomething() { }
}
// class B decides not to override base class method still this method is available in class C to override.
Now if this user wants no further availability of DoSomething method in classes inherited by class C then it could be acheived using sealed keyword.

That's it for this article. Thanks for reading this, please share your feedback in the comments section below.