Method Overriding

Method overriding in C# is a feature that allows a subclass (derived class) to provide a specific implementation for a method that is already defined in its superclass (base class). This is a fundamental concept in object-oriented programming (OOP) that supports polymorphism, enabling you to call the same method on different objects and have them behave differently based on their actual types.

Basics of Method Overriding

To override a method in C#, the base class must first define the method as virtual, abstract, or provide an implementation. The derived class then uses the override keyword to specify its own implementation of the method.

Example

using System;

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

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

class Program
{
  static void Main()
  {
      // Creating an instance of the base class
      Animal genericAnimal = new Animal();
      genericAnimal.Speak(); // Outputs: The animal makes a sound.

        // Creating an instance of the derived class
      Dog specificDog = new Dog();
      specificDog.Speak(); // Outputs: The dog barks.

        // Demonstrating polymorphism
      Animal polymorphicDog = new Dog();
      polymorphicDog.Speak(); // Outputs: The dog barks.
  }
}

Explanation of the Example

  1. Base Class (Animal):
    • The Animal class has a method Speak marked as virtual. This means the method can be overridden in any derived class.
  2. Derived Class (Dog):
    • The Dog class inherits from Animal and overrides the Speak method using the override keyword. The new implementation prints “The dog barks.”
  3. Main Method:
    • We create an instance of Animal and call the Speak method, which prints “The animal makes a sound.”
    • We create an instance of Dog and call the Speak method, which prints “The dog barks.”
    • We also demonstrate polymorphism by creating an Animal reference that points to a Dog object. Calling Speak on this reference invokes the overridden method in the Dog class, printing “The dog barks.”

Key Points About Method Overriding

  1. Virtual Methods:
    • The base class method must be marked as virtual, abstract, or override to be overridden in a derived class.
  2. Override Keyword:
    • The derived class method must use the override keyword to provide a new implementation.
  3. Polymorphism:
    • Method overriding is a key aspect of polymorphism, allowing a base class reference to invoke methods in derived class instances.
  4. Access Modifiers:
    • The access level (e.g., public, protected) of the overriding method must match the overridden method’s access level.

Benefits of Method Overriding

  1. Runtime Polymorphism:
    • Allows objects to be treated as instances of their base class, enabling more flexible and reusable code.
  2. Code Reusability:
    • Base class functionality can be extended or modified in derived classes without altering the base class code.
  3. Consistency:
    • Ensures that the derived class provides specific behavior while maintaining a consistent interface.
Post a comment

Leave a Comment

Scroll to Top