Method Overriding in C#

What is Method Overriding?

Method overriding in C# allows a derived class to provide a specific implementation of a method that is already defined in its base class. It is used to redefine a method in the derived class, giving it specialized behavior.

Why Use Method Overriding?

Method overriding allows for polymorphism, where the same method can behave differently in derived classes. It helps provide flexibility and extensibility to a system by allowing derived classes to modify base class behavior.

Example: Method Overriding in C#

Below is an example of method overriding where the Animal class provides a generic implementation of the MakeSound method, and the Dog class overrides this method with its own implementation.

Example Code:

public class Animal
{
    // Virtual method
    public virtual void MakeSound()
    {
        Console.WriteLine("The animal makes a sound.");
    }
}

public class Dog : Animal
{
    // Override the virtual method in the derived class
    public override void MakeSound()
    {
        Console.WriteLine("The dog barks.");
    }
}

// Usage
Animal animal = new Animal();
animal.MakeSound();  // Output: The animal makes a sound.

Dog dog = new Dog();
dog.MakeSound();  // Output: The dog barks.

        

In this example, the Dog class overrides the MakeSound method of the Animal class to provide its own implementation. The base class method is marked with the virtual keyword, and the derived class method uses the override keyword to indicate it is providing a new implementation.

Virtual and Override Keywords

  • virtual: The virtual keyword is used in the base class to mark a method that can be overridden by derived classes.
  • override: The override keyword is used in the derived class to replace the base class implementation with its own.

Rules for Method Overriding

  • The method in the base class must be marked with the virtual keyword.
  • The method in the derived class must use the override keyword.
  • The overridden method must have the same signature as the method in the base class (same name, parameters, and return type).
  • Only methods that are marked as virtual, abstract, or override can be overridden.

Method Overriding vs. Method Overloading

While both method overriding and method overloading allow methods to have the same name, they differ in purpose:

  • Method Overriding: Involves a derived class providing its own implementation of a base class method. It requires virtual in the base class and override in the derived class.
  • Method Overloading: Allows a class to have multiple methods with the same name but different parameter lists within the same class. No inheritance is required.

Key Points to Remember

  • Method overriding enables polymorphism, allowing derived classes to provide their own implementation of base class methods.
  • The virtual keyword marks a method that can be overridden, while the override keyword is used to define the new implementation.
  • Method overriding is resolved at runtime, enabling dynamic method invocation based on the object's actual type.