Method Overriding In C#
Maybe your like
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 scenarioAfter making a few modifications as in the following:
Console.WriteLine("subClass Saying Hello!"); base.Greetings(); // after modificationsNow 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.
Tag » What Is Overriding In C#
-
C# | Method Overriding - GeeksforGeeks
-
Override Modifier - C# Reference - Microsoft Docs
-
C# Method Overriding - Javatpoint
-
Method Overriding In C#.NET
-
Method Overriding In C# Examples - Dot Net Tutorials
-
C# Method Overriding - Tutlane
-
Types Of Overriding In C# With Examples - EduCBA
-
Overloading Vs. Overriding In C# | HackerNoon
-
C Sharp Method Overriding - W3schools.blog
-
How To Override Methods In C#. Method Overriding, In ... - Medium
-
Understanding Virtual, Override And New Keyword In C# - DotNetTricks
-
What Is Override? - Definition From Techopedia
-
C# 오버라이딩(Overriding) 개념 이해 - Link2Me
-
Overriding In C# - Tutorialspoint