C# Keywords Tutorial Part 96: virtual

C# Keywords Tutorial Part 96: virtual

C# is an object-oriented programming language that provides the “virtual” keyword to enable polymorphism and runtime method invocation. In this blog post, we will discuss the use of “virtual” in C# and provide code examples to illustrate its behavior.

What is the “virtual” keyword in C#?

The keyword “virtual” can be used in C# to change a method, property, or indexer declaration. A method can be overridden by a subclass if it is designated as virtual. A subclass may supply its own implementation of the method as a result of the polymorphism and runtime method invocation enabled by this.

Here is an example of a virtual method in C#:

public class Animal
{
    public virtual void Speak()
    {
        Console.WriteLine("The animal makes a sound");
    }
}

public class Cat : Animal
{
    public override void Speak()
    {
        Console.WriteLine("The cat meows");
    }
}

Animal animal = new Animal();
Cat cat = new Cat();

animal.Speak(); // Output: The animal makes a sound
cat.Speak(); // Output: The cat meows

In this example, the “Speak” method in the “Animal” class is marked as virtual. This allows the “Cat” class to override it and provide its own implementation of the method. When we create instances of both classes and call the “Speak” method on each of them, we can see that the output differs based on the implementation of the method in each class.

What happens if a virtual method is not overridden?

If a virtual method is not overridden by a subclass, the implementation of the method in the base class is used instead. Here is an example:

public class Dog : Animal
{
}

Dog dog = new Dog();

dog.Speak(); // Output: The animal makes a sound

In this example, we have created a “Dog” class that inherits from the “Animal” class but does not override the “Speak” method. When we call the “Speak” method on an instance of the “Dog” class, the output is the same as when we called it on an instance of the “Animal” class.

What is the “sealed” keyword in C#?

In C#, the “sealed” keyword can be used to prevent a virtual method from being overridden by a subclass. Here is an example:

public class Cow : Animal
{
    public sealed override void Speak()
    {
        Console.WriteLine("The cow moos");
    }
}

public class Bull : Cow
{
    // This will result in a compile-time error
    // because the Speak method in Cow is sealed.
    public override void Speak()
    {
    }
}

Cow cow = new Cow();
Bull bull = new Bull();

cow.Speak(); // Output: The cow moos
bull.Speak(); // Output: The cow moos

In this example, the “Speak” method in the “Cow” class is marked as sealed, preventing any subclasses from overriding it. When we create a “Bull” class that attempts to override the method, we get a compile-time error. When we create instances of both classes and call the “Speak” method on each of them, we can see that the output is the same for both classes because the “Speak” method in “Cow” cannot be overridden.

Share this post

Leave a Reply

Your email address will not be published. Required fields are marked *