Method Overriding
The term “Method Overriding” refers to the ability to override or supersede methods of a base class in derived classes. This allows derived classes to define specific implementations of methods defined in base classes.
To implement the method overriding mechanism, you need to use the virtual keyword when defining the method in the base class, and the override keyword when defining the method in the derived class. For example:
using System;
class BaseClass
{
public virtual void Method()
{
Console.WriteLine("Hello from the base class!");
}
}
class DerivedClass1 : BaseClass
{
public override void Method()
{
Console.WriteLine("Hello from derived class 1!");
}
}
class DerivedClass2 : BaseClass
{
public override void Method()
{
Console.WriteLine("Hello from derived class 2!");
}
}
class Program
{
static void Main()
{
BaseClass baseObj = new BaseClass();
DerivedClass1 der1 = new DerivedClass1();
DerivedClass2 der2 = new DerivedClass2();
baseObj.Method();
der1.Method();
der2.Method();
}
}
The console will output…
Hello from the base class!
Hello from derived class 1!
Hello from derived class 2!…because the virtual method Method() of the base class is overridden in the derived classes with the same name.
Even though you override the virtual method of the base class in the derived class, you can still access the base class virtual method from within the derived class using the base keyword. For example:
using System;
class BaseClass
{
public virtual void Method()
{
Console.WriteLine("Hello from the base class!");
}
}
class DerivedClass : BaseClass
{
public override void Method()
{
base.Method();
Console.WriteLine("Hello from the derived class!");
}
}
class Program
{
static void Main()
{
DerivedClass der = new DerivedClass();
der.Method();
}
}
Now the console will output…
Hello from the base class!
Hello from the derived class!…because the overridden method in the derived class first calls the virtual method of the base class.
Also, an object of the base class type can reference an object of a derived class. For example:
using System;
class BaseClass
{
public virtual void Method()
{
Console.WriteLine("Hello from the base class!");
}
}
class DerivedClass : BaseClass
{
public override void Method()
{
Console.WriteLine("Hello from the derived class!");
}
}
class Program
{
static void Main()
{
BaseClass baseObj = new DerivedClass();
baseObj.Method();
}
}
The console will display the text printed by the overridden method:
Hello from the derived class!The reverse is not true. An object of a derived class type cannot directly reference an object of the base class, because the derived class may contain additional members that do not exist in the base class. If an object of a derived class type could reference an object of the base class, it would lead to runtime errors, as the derived class would attempt to access members that do not exist in the base class.
Although it seems quite simple, the ability to override base class methods in derived classes allows you to treat different objects in a unified way, but with specific behavior — that is, it gives you the ability to implement polymorphic behavior of objects. Code that implements method overriding is far more flexible and modular, because class-specific logic can be encapsulated within the methods of that class.