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#.
What is polymorphism?
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.
Method overriding example
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
Method hiding example
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.
Prevent derived class from overriding virtual method
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.