Why need to use "virtual" keyword when we can use "new" keyword

The virtual and new keywords serve different purposes and have different implications in object-oriented programming.

  1. virtual Keyword:

    • When a method, property, indexer, or event is marked as virtual in the base class, it allows derived classes to provide their own implementation for that member using the override keyword.

    • Using virtual indicates that the member is designed to be overridden in derived classes, promoting polymorphic behavior where the specific implementation is determined at runtime based on the type of the object.

  2. new Keyword:

    • The new keyword allows derived classes to hide members of the same name and signature from the base class without overriding them. It essentially creates a new member in the derived class that happens to have the same name as a member in the base class.

    • Unlike override, new does not participate in polymorphism. When you call a method using the base class reference but the actual object is of the derived class, the behavior depends on the reference type. If the reference type is of the base class, it calls the base class member. If the reference type is of the derived class, it calls the derived class member.

So, the reason why you might want to use virtual instead of new is to enable polymorphic behavior and allow derived classes to provide their own implementations of a member. This is particularly useful in scenarios where you want to define a common interface or behavior in the base class but allow different implementations in derived classes. On the other hand, new is used when you want to hide a member from the base class in the derived class without overriding it and without polymorphic behavior. Each has its own use case depending on the desired behavior and design of your classes.

Example:

Sure, let's illustrate the difference between using virtual and new with an example:

Suppose we have a base class Animal with a method MakeSound, and we want to allow derived classes to provide their own implementations of this method.

  1. Using virtual and override:

using System;

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

public class Dog : Animal
{
    public override void MakeSound()
    {
        Console.WriteLine("Dog barks");
    }
}

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

public class Program
{
    public static void Main()
    {
        Animal animal1 = new Animal();
        animal1.MakeSound(); // Output: Animal makes a sound

        Animal animal2 = new Dog();
        animal2.MakeSound(); // Output: Dog barks

        Animal animal3 = new Cat();
        animal3.MakeSound(); // Output: Cat meows
    }
}

In this example:

  • The MakeSound method in the Animal class is marked as virtual, indicating that it can be overridden in derived classes.

  • Both Dog and Cat classes override the MakeSound method with their own implementations.

  • When calling MakeSound on objects of type Animal, the actual implementation called depends on the runtime type of the object. This is polymorphic behavior.

  1. Using new:

using System;

public class Animal
{
    public void MakeSound()
    {
        Console.WriteLine("Animal makes a sound");
    }
}

public class Dog : Animal
{
    public new void MakeSound()
    {
        Console.WriteLine("Dog barks");
    }
}

public class Cat : Animal
{
    public new void MakeSound()
    {
        Console.WriteLine("Cat meows");
    }
}

public class Program
{
    public static void Main()
    {
        Animal animal1 = new Animal();
        animal1.MakeSound(); // Output: Animal makes a sound

        Animal animal2 = new Dog();
        animal2.MakeSound(); // Output: Animal makes a sound (base class method is called)

        Animal animal3 = new Cat();
        animal3.MakeSound(); // Output: Animal makes a sound (base class method is called)
    }
}

In this example:

  • The MakeSound method in both Dog and Cat classes is marked with the new keyword, indicating that they are hiding the method from the base class without overriding it.

  • When calling MakeSound on objects of type Animal, regardless of the actual runtime type of the object, the method from the base class is always called. This is because new does not participate in polymorphism; the method called is determined by the compile-time type of the reference.

Last updated