Method Overriding in C#

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:
 
Overriding Explanation

Then there must be ring facility in it:
 
Overriding method 
 
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).
 
Overriding with different functionality 
 
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.
 
method overriding 
 

Keywords in Method Overriding

 
There are the following 3 types of keywords used in C# for method overriding:
 
Keywords in Method Overriding 
 
Virtual Keyword
 
It tells the compiler that this method can be overridden by derived classes.
  1. public virtual int myValue()  
  2. {  
  3.     -  
  4.     -  
  5.     -  
  6. }  
Override Keyword
 
In the subclass, it tells the compiler that this method is overriding the same named method in the base class.
  1. public override int myValue()  
  2. {  
  3.      -  
  4.      -  
  5.      -  
  6. }  
Base Keyword
 
In the subclass, it calls the base class method for overriding functionality.
  1. base.myValue();  

Method Overriding Example

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. namespace Hello_Word  
  6. {  
  7.     class baseClass  
  8.     {  
  9.         public virtual void Greetings()  
  10.         {  
  11.             Console.WriteLine("baseClass Saying Hello!");  
  12.         }  
  13.     }  
  14.     class subClass : baseClass  
  15.     {  
  16.         public override void Greetings()  
  17.         {  
  18.             base.Greetings();  
  19.             Console.WriteLine("subClass Saying Hello!");  
  20.         }  
  21.     }  
  22.     class Program  
  23.     {  
  24.         static void Main(string[] args)  
  25.         {  
  26.             baseClass obj1 = new subClass();  
  27.             obj1.Greetings();  
  28.             Console.ReadLine();  
  29.         }  
  30.     }  
  31. }  
method overriding in c# 
 
An output window is showing that baseClass will get called first and then subclass. If we make certain changes in this code like:
  1. base.Greetings();  
  2. Console.WriteLine("subClass Saying Hello!"); // current scenario  
After making a few modifications as in the following:
  1. Console.WriteLine("subClass Saying Hello!");  
  2. base.Greetings(); // after modifications  
Now our output will be like this:
 
method overriding in c#

If we remove "base.Greetings();" then the compiler will not call the baseclass as in the following:
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. namespace Hello_Word  
  6. {  
  7.     class baseClass  
  8.     {  
  9.         public virtual void Greetings()  
  10.         {  
  11.             Console.WriteLine("baseClass Saying Hello!");  
  12.         }  
  13.     }  
  14.     class subClass : baseClass  
  15.     {  
  16.         public override void Greetings()  
  17.         {  
  18.             Console.WriteLine("subClass Saying Hello!");  
  19.             // base.Greetings();  
  20.         }  
  21.     }  
  22.     class Program  
  23.     {  
  24.         static void Main(string[] args)  
  25.         {  
  26.             baseClass obj1 = new subClass();  
  27.             obj1.Greetings();  
  28.             Console.ReadLine();  
  29.         }  
  30.     }  
  31. }  
method overriding in c# 
 
Now making some other changes, like placing:
  1. baseClass obj1 = new baseClass();  
  2. obj1.Greetings();  
Instead of:
  1. baseClass obj1 = new subClass();  
  2. obj1.Greetings();  
Now let’s check the output window, what’s its showing now:
 
method overriding in c# 
 
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.