This article explains method overriding and its various applications using examples in C#.
Method Overriding
Overriding can be defined as: being able to change or augment the behavior of methods in classes, known as overriding their logic; it is one of the most powerful aspects of Object Oriented Programming.
Explanation
Suppose you have a Phone, no please don’t suppose everyone has a phone today, although some people even have more than one. Okay leave it, so if you have phone:
Then there must be ring facility in it:
Now your phone can be of any type, like it can be cellular, satellite or landline, these all types of phones will also have the same or a different functionality (based on their attribute).
Now whenever you receive a call, the caller doesn’t know whether you have a cellular phone, landline phone or anything else, he/she just calls you and according to that call operation your phone rings. In the case of method overriding, your phone works as a class and the ring is the functionality.
Keywords in Method Overriding
There are the following 3 types of keywords used in C# for method overriding:
Virtual Keyword
It tells the compiler that this method can be overridden by derived classes.
public virtual int myValue()
{
-
-
-
}
Override Keyword
In the subclass, it tells the compiler that this method is overriding the same named method in the base class.
public override int myValue()
{
-
-
-
}
Base Keyword
In the subclass, it calls the base class method for overriding functionality.
base.myValue();
Method Overriding Example
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Hello_Word
{
class baseClass
{
public virtual void Greetings()
{
Console.WriteLine("baseClass Saying Hello!");
}
}
class subClass : baseClass
{
public override void Greetings()
{
base.Greetings();
Console.WriteLine("subClass Saying Hello!");
}
}
class Program
{
static void Main(string[] args)
{
baseClass obj1 = new subClass();
obj1.Greetings();
Console.ReadLine();
}
}
}
An output window is showing that baseClass will get called first and then subclass. If we make certain changes in this code like:
base.Greetings();
Console.WriteLine("subClass Saying Hello!"); // current scenario
After making a few modifications as in the following:
Console.WriteLine("subClass Saying Hello!");
base.Greetings(); // after modifications
Now our output will be like this:
If we remove "base.Greetings();" then the compiler will not call the baseclass as in the following:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Hello_Word
{
class baseClass
{
public virtual void Greetings()
{
Console.WriteLine("baseClass Saying Hello!");
}
}
class subClass : baseClass
{
public override void Greetings()
{
Console.WriteLine("subClass Saying Hello!");
// base.Greetings();
}
}
class Program
{
static void Main(string[] args)
{
baseClass obj1 = new subClass();
obj1.Greetings();
Console.ReadLine();
}
}
}
Now making some other changes, like placing:
baseClass obj1 = new baseClass();
obj1.Greetings();
Instead of:
baseClass obj1 = new subClass();
obj1.Greetings();
Now let’s check the output window, what’s its showing now:
So as you are seeing that now its calling only the baseClass. So now I guess you will be a little closer to the method overriding functionality.