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
usingSystem;
classAnimal { // Base class method marked as virtual publicvirtualvoidSpeak() { Console.WriteLine("The animal makes a sound."); } }
classDog : Animal { // Overriding the Speak method in the derived class publicoverridevoidSpeak() { Console.WriteLine("The dog barks."); } }
classProgram { staticvoidMain() { // Creating an instance of the base class AnimalgenericAnimal=newAnimal(); genericAnimal.Speak(); // Outputs: The animal makes a sound.
// Creating an instance of the derived class DogspecificDog=newDog(); specificDog.Speak(); // Outputs: The dog barks.
// Demonstrating polymorphism AnimalpolymorphicDog=newDog(); polymorphicDog.Speak(); // Outputs: The dog barks. } }
Explanation of the Example
Base Class (Animal):
The Animal class has a method Speak marked as virtual. This means the method can be overridden in any derived class.
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.”
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
Virtual Methods:
The base class method must be marked as virtual, abstract, or override to be overridden in a derived class.
Override Keyword:
The derived class method must use the override keyword to provide a new implementation.
Polymorphism:
Method overriding is a key aspect of polymorphism, allowing a base class reference to invoke methods in derived class instances.
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
Runtime Polymorphism:
Allows objects to be treated as instances of their base class, enabling more flexible and reusable code.
Code Reusability:
Base class functionality can be extended or modified in derived classes without altering the base class code.
Consistency:
Ensures that the derived class provides specific behavior while maintaining a consistent interface.