C# Method Overriding (virtual and override


Method overriding in C# allows a derived class to provide a specific implementation of a method that is already defined in its base class. This enables polymorphism, where the method that gets executed is determined at runtime based on the object type, not the reference type. To use method overriding, you must define the base class method with the virtual keyword and override it in the derived class using the override keyword.

Key Concepts

  1. Virtual Method: A method in a base class that is declared with the virtual keyword, indicating that it can be overridden in any derived class.

  2. Override Method: A method in a derived class that is declared with the override keyword, providing a new implementation of the virtual method from the base class.

Example of Method Overriding

Here's an example to illustrate method overriding in C#:

// Base class public class Animal { public virtual void Speak() { Console.WriteLine("Animal makes a sound."); } } // Derived class public class Dog : Animal { public override void Speak() { Console.WriteLine("Dog barks."); } } // Another derived class public class Cat : Animal { public override void Speak() { Console.WriteLine("Cat meows."); } } // Usage public class Program { public static void Main() { Animal myDog = new Dog(); myDog.Speak(); // Outputs: Dog barks. Animal myCat = new Cat(); myCat.Speak(); // Outputs: Cat meows. } }

Explanation of the Example

  1. Base Class Animal:

    • The Animal class defines a virtual method Speak(), which outputs a generic message. This method is intended to be overridden by derived classes.
  2. Derived Class Dog:

    • The Dog class inherits from the Animal class and overrides the Speak() method using the override keyword to provide its specific implementation (barking).
  3. Derived Class Cat:

    • The Cat class also inherits from the Animal class and overrides the Speak() method to define its own behavior (meowing).
  4. Main Method:

    • In the Main() method, instances of Dog and Cat are created using the base class Animal reference.
    • When Speak() is called on these references, the overridden methods in the respective derived classes are executed, demonstrating polymorphism.

Benefits of Method Overriding

  • Polymorphism: Enables calling methods on base class references that behave differently based on the actual object type at runtime, making code more flexible and extensible.
  • Code Reusability: Base classes can define default behavior while allowing derived classes to customize specific functionalities without changing the base class.
  • Maintainability: Changes made in the base class can automatically propagate to derived classes, simplifying maintenance and reducing code duplication.

Limitations of Method Overriding

  • Complexity: Overriding methods can lead to complex hierarchies, making the code harder to follow if not designed carefully.
  • Increased Dependency: Derived classes are dependent on the base class implementation. Changes in the base class can affect all derived classes.
  • Restricted Access: If a base class method is marked as sealed, it cannot be overridden in further derived classes, which may limit flexibility.

Summary

Method overriding in C# allows derived classes to provide specific implementations for methods defined in base classes, promoting polymorphism and enabling a more flexible and maintainable code structure. By using the virtual and override keywords, developers can customize behavior while retaining common functionality in the base class. However, care should be taken to manage complexity and dependencies within the class hierarchy.