Virtual vs Override in C#

 

                                            Virtual vs Override

1. virtual Keyword

The virtual keyword is used in the base class to indicate that a method or property can be overridden in derived classes.

Key Points:

  • It is used to mark a method or property in the base class that can be overridden by any derived class.
  • A method or property marked as virtual has a default implementation (it can still have code, but it can also be overridden in derived classes).
  • If a derived class doesn't override the virtual method, the base class method implementation is used.
  • It allows polymorphic behavior, meaning that a method call to a virtual method can be dynamically dispatched to the appropriate derived class method at runtime.

Example:


public class Animal { // This method is virtual, and can be overridden in derived classes. public virtual void Speak() { Console.WriteLine("Animal makes a sound"); } } public class Dog : Animal { // Override the base class method to provide specific behavior. public override void Speak() { Console.WriteLine("Dog barks"); } } public class Program { public static void Main() { Animal myAnimal = new Animal(); myAnimal.Speak(); // Outputs: Animal makes a sound Dog myDog = new Dog(); myDog.Speak(); // Outputs: Dog barks } }

2. override Keyword

The override keyword is used in the derived class to provide a new implementation for a method that was previously defined as virtual in a base class (or as abstract in an abstract class).

Key Points:

  • The override keyword is used to replace or modify the implementation of a virtual method from the base class in a derived class.
  • The method signature in the derived class must match the signature of the method in the base class (name, return type, and parameters).
  • The override keyword ensures that the compiler knows the method is intentionally overriding the base class method.
  • The override method can call the base class method using base.MethodName() if needed.

Example:


public class Animal { public virtual void Speak() { Console.WriteLine("Animal makes a sound"); } } public class Dog : Animal { public override void Speak() { Console.WriteLine("Dog barks"); } } public class Program { public static void Main() { Animal myAnimal = new Animal(); myAnimal.Speak(); // Outputs: Animal makes a sound Dog myDog = new Dog(); myDog.Speak(); // Outputs: Dog barks } }

Key Differences Between virtual and override:

Featurevirtualoverride
Where it's usedUsed in the base class to allow a method to be overridden.Used in the derived class to provide a specific implementation of a method defined in the base class.
PurposeTo declare a method as overridable (i.e., it can be overridden in derived classes).To override an inherited virtual method with a new implementation in the derived class.
Method ImplementationContains the default implementation (optional).Contains a new implementation that replaces the base class implementation.
Calling Base MethodN/A — base class method can be overridden by derived classes.Can call the base class method using base.MethodName().
InheritanceCan be inherited by derived classes.Can only be used to override methods marked as virtual or abstract in the base class.
PolymorphismAllows polymorphic behavior (the method can be overridden in derived classes).Enables polymorphic behavior by providing a specialized implementation in the derived class.

More Detailed Example with base Keyword:

You can use the base keyword to call the base class method from the derived class method. This is useful when you want to retain the base class behavior in addition to adding new functionality in the derived class.


public class Animal { public virtual void Speak() { Console.WriteLine("Animal makes a sound"); } } public class Dog : Animal { public override void Speak() { // Calling the base class method base.Speak(); Console.WriteLine("Dog barks"); } } public class Program { public static void Main() { Dog myDog = new Dog(); myDog.Speak(); // Outputs: // Animal makes a sound // Dog barks } }

Abstract Classes and override:

  • Abstract classes can contain abstract methods that must be implemented by derived classes. These abstract methods are implicitly virtual, and derived classes are required to provide an implementation using the override keyword.
  • A class that contains an abstract method cannot be instantiated directly.

public abstract class Animal { public abstract void Speak(); // No body, must be overridden } public class Dog : Animal { public override void Speak() // Must provide an implementation { Console.WriteLine("Dog barks"); } } public class Program { public static void Main() { Animal myAnimal = new Dog(); myAnimal.Speak(); // Outputs: Dog barks } }

Virtual and Override with Return Types:

You can override methods with return types in a similar way as with methods without return types.

Example:


public class Animal { public virtual string GetName() { return "Animal"; } } public class Dog : Animal { public override string GetName() { return "Dog"; } } public class Program { public static void Main() { Animal myAnimal = new Dog(); Console.WriteLine(myAnimal.GetName()); // Outputs: Dog } }

Summary:

  • virtual: Used in the base class to declare a method that can be overridden by derived classes.
  • override: Used in the derived class to provide a new implementation for a method that was marked as virtual (or abstract) in the base class.

Using virtual and override together allows for the implementation of polymorphism, where the correct method is chosen at runtime based on the object type, not the reference type. This enables more flexible and dynamic behavior in object-oriented programming.

Comments

Popular posts from this blog

Performance Optimization in Sitecore

Strategies for Migrating to Sitecore from legacy or upgrading from older Sitecore

Azure Event Grid Sample code