What is the Override Function?
In C#, the override keyword is used in object-oriented programming (OOP) to modify or extend the implementation of a method defined in a base class. This functionality enables derived classes to implement their specific method while maintaining a consistent interface.
When you mark a method in a base class as virtual, it signals that it can be overridden in any derived class. The derived class then uses the override keyword to provide its implementation of the method. Overriding is particularly useful when you need a derived class to change or extend the behavior of a base class method while still adhering to the structure defined by the base class.
Syntax
public class BaseClass
{
public virtual void DisplayMessage()
{
Console.WriteLine("Message from BaseClass.");
}
}
public class DerivedClass : BaseClass
{
public override void DisplayMessage()
{
Console.WriteLine("Message from DerivedClass.");
}
}
In this example
- The BaseClass defines a method DisplayMessage() as virtual.
- The DerivedClass overrides the DisplayMessage() method using the override keyword to provide its implementation.
Why Use Override?
- To customize or extend the behavior of a base class in a derived class.
- Ensure that derived classes adhere to a consistent interface while providing specific implementations.
- To enable runtime polymorphism, the executed method is determined at runtime based on the object type.
Role of Override in OOP Concepts
The override function plays a significant role in the following OOP concepts.
- 1. Polymorphism
- 2. Inheritance
- 3. Encapsulation
1. Polymorphism
Polymorphism allows objects of different types to be treated as objects of a common base type. By overriding methods, you can define specific behaviors for derived classes while interacting with them through a common interface.
Example. A method call on a base class reference can dynamically execute the overridden method in the derived class at runtime.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Override_Polymorphism
{
public class Employee
{
public virtual void Work()
{
Console.WriteLine("Employee is working.");
Console.WriteLine(); // Adds an empty line
}
}
public class Developer : Employee
{
public override void Work()
{
Console.WriteLine("Developer is writing code.");
Console.WriteLine(); // Adds an empty line
}
}
public class Manager : Employee
{
public override void Work()
{
Console.WriteLine("Manager is managing the team.");
Console.WriteLine(); // Adds an empty line
}
}
class Program
{
static void Main()
{
Employee emp1 = new Developer();
Employee emp2 = new Manager();
emp1.Work();
emp2.Work();
}
}
}

Picture 01. Here, polymorphism is demonstrated as the Work method behaves differently based on the object type, even when accessed through the base class reference.
2. Inheritance
Inheritance enables a class to inherit members from a base class. The override keyword lets you modify or extend these members to meet the requirements of the derived class.
Example. A base class might define general behavior, while derived classes implement specific variations of that behavior.



Join the conversation! Your thoughts help the community grow.