Click here to Skip to main content
15,861,125 members
Articles / Programming Languages / C#
Article

Method Overriding in C#

Rate me:
Please Sign up or sign in to vote.
4.02/5 (135 votes)
9 May 20074 min read 679.9K   98   74
A tutorial on method overriding in C#

Introduction

Method overriding in C# is a feature like the virtual function in C++. Method overriding is a feature that allows you to invoke functions (that have the same signatures) that belong to different classes in the same hierarchy of inheritance using the base class reference. C# makes use of two keywords: virtual and overrides to accomplish Method overriding. Let's understand this through small examples.

P1.cs

C#
class BC
{
  public void Display()
  {
     System.Console.WriteLine("BC::Display");
  }
}

class DC : BC
{
  new public void Display()
  {
     System.Console.WriteLine("DC::Display");
  }
}

class Demo
{
  public static void Main()
  {
     BC b;
     b = new BC();
     b.Display();    
  }
}

Output

BC::Display

The above program compiles and runs successfully to give the desired output. It consists of a base class BC and a derived class DC. Class BC consists of function Display(). Class DC hides the function Display() it inherited from the base class BC by providing its on implementatin of Display(). Class Demo consists of entrypoint function Main(). Inside Main() we first create a reference b of type BC. Then we create an object of type BC and assign its reference to reference variable b. Using the reference variable b we invoke the function Display(). As expected, Display() of class BC is executed because the reference variable b refers to the object of class BC.

Now we add a twist of lime to the above program.

P2.cs

C#
class BC
{
  public void Display()
  {
     System.Console.WriteLine("BC::Display");
  }
}

class DC : BC
{
  new public void Display()
  {
     System.Console.WriteLine("DC::Display");
  }
}

class Demo
{
  public static void Main()
  {
     BC b;
     b = new BC();
     b.Display();    

     b = new DC();
     b.Display();    
  }
}

Output

BC::Display
BC::Display

Here we are creating an object of Derived class DC and storing its reference in the reference variable b of type BC. This is valid in C#. Next, using the reference variable b we invoke the function Display(). Since b contains a reference to object of type DC one would expect the function Display() of class DC to get executed. But that does not happen. Instead what is executed is the Display() of BC class. That's because the function is invoked based on type of the reference and not to what the reference variable b refers to. Since b is a reference of type BC, the function Display() of class BC will be invoked, no matter whom b refers to. Take one more example.

P3.cs

C#
class BC
{
  public void Display()
  {
     System.Console.WriteLine("BC::Display");
  }
}

class DC : BC
{
  new public void Display()
  {
     System.Console.WriteLine("DC::Display");
  }
}

class TC : BC
{
  new public void Display()
  {
     System.Console.WriteLine("DC::Display");
  }
}

class Demo
{
  public static void Main()
  {
     BC b;
     b = new BC();
     b.Display();    

     b = new DC();
     b.Display();    

     b = new TC();
     b.Display();    
  }
}

Output

BC::Display
BC::Display
BC::Display

The output of the above program is a receipt of the fact that no matter to whom base class reference b refers, it invokes the functions of the class that matches its type. But doesn't this sound absurd? If b contains the reference to a perticular derived class object, then its supposed to invoke the function of that class. Well, C# helps us do this by the usage of keywords virtual and override as shown in the following program.

P4.cs

C#
class BC
{
  public virtual void Display()
  {
     System.Console.WriteLine("BC::Display");
  }
}

class DC : BC
{
  public override void Display()
  {
     System.Console.WriteLine("DC::Display");
  }
}

class Demo
{
  public static void Main()
  {
     BC b;
     b = new BC();
     b.Display();    

     b = new DC();
     b.Display();    
  }
}

Output

BC::Display
DC::Display

The above program compiles and runs successfully to give the expected desired output. The function Display() of Base class BC is declared as virtual, while the Derived class's implementation of Display() is decorated with the modifier override. Doing so enables C# to invoke functions like Display() based on objects the reference variable refers to and not the type of reference that is invoking the function. Hence in the above program when b refers to the object of class BC it invokes Display() of BC and then when b refers to the object of class DC it invokes Display() of class DC. Let's see if this holds true for the third generation of derived classes. Take the following program.

P4.cs

C#
class BC
{
  public virtual void Display()
  {
     System.Console.WriteLine("BC::Display");
  }
}

class DC : BC
{
  public override void Display()
  {
     System.Console.WriteLine("DC::Display");
  }
}

class TC : DC
{
  public override void Display()
  {
     System.Console.WriteLine("TC::Display");
  }
}

class Demo
{
  public static void Main()
  {
     BC b;
     b = new BC();
     b.Display();    

     b = new DC();
     b.Display();    

     b = new TC();
     b.Display();    
  }
}

Output

BC::Display
DC::Display
TC::Display

The above program compiles and runs successfully to give the expected desired output. The function Display() of Base class BC is declared as virtual, while the implementation of Display() in successive Derived classes is decorated with the modifier override. Next, we succesively create objects of each class and store their reference in base class reference variable b and invoke Display(). The rite versions of Display get invoked based on the object the reference variable refers to. Time for a tiny teaser! Guess what the output would be in the following program?

P5.cs

C#
class BC
{
  public virtual void Display()
  {
     System.Console.WriteLine("BC::Display");
  }
}

class DC : BC
{
  public override void Display()
  {
     System.Console.WriteLine("DC::Display");
  }
}

class TC : DC
{

}

class Demo
{
  public static void Main()
  {
     BC b;

     b = new TC();
     b.Display();    
  }
}

Output

DC::Display

Since TC has no implementation of Display(), it inherits Display() from DC as TC is derived from DC. Hence Display() from Derived class DC gets executed. It's as if the derived class TC looked like this:

C#
class TC 
{
  public override void Display()
  {
     System.Console.WriteLine("DC::Display");
  }
}
to the compiler. Take one more example. Guess what its output will be.

P6.cs

C#
class BC
{
  public virtual void Display()
  {
     System.Console.WriteLine("BC::Display");
  }
}

class DC : BC
{
  public override void Display()
  {
     System.Console.WriteLine("DC::Display");
  }
}

class TC : DC
{
  public new void Display()
  {
     System.Console.WriteLine("TC::Display");
  }
}

class Demo
{
  public static void Main()
  {
     BC b;

     b = new TC();
     b.Display();    
  }
}

Output

DC::Display

Agreed that TC defines its own new version of Display(). But its version of display is not invoked as Display() of TC does not override the Display() of the base class. With this understood we are done with Method overriding in C#.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Software Developer (Senior)
India India
I am a Software engineer with around 7+ years of experience. Most of my experience is in Storage technology.

Comments and Discussions

 
PraiseEasy to Understand Pin
Oyeyinka Onaolapo18-Jun-19 2:00
Oyeyinka Onaolapo18-Jun-19 2:00 
Questionlearnt a very new thing.. Pin
Member 1303984322-Mar-17 21:49
Member 1303984322-Mar-17 21:49 
GeneralMy vote of 5 Pin
Member 1236036716-Jun-16 8:07
Member 1236036716-Jun-16 8:07 
Questionmethod overriding Pin
Member 1207477820-Oct-15 17:27
Member 1207477820-Oct-15 17:27 
AnswerRe: method overriding Pin
Chetan Kudalkar3-Nov-15 18:23
Chetan Kudalkar3-Nov-15 18:23 
QuestionMethod Overriding in C# Pin
Lokesh Acharaya19-Oct-15 18:43
Lokesh Acharaya19-Oct-15 18:43 
GeneralSimply Superb Pin
Mayank Kukadia26-Jun-15 9:23
Mayank Kukadia26-Jun-15 9:23 
GeneralVery Good Explanation Pin
Kunal R. Khairnar1-Mar-15 22:41
Kunal R. Khairnar1-Mar-15 22:41 
GeneralMy vote of 1 Pin
abhay5498-Jul-14 16:59
abhay5498-Jul-14 16:59 
QuestionMy Vote 5 Pin
niknishu18-May-14 8:05
niknishu18-May-14 8:05 
GeneralMy vote of 1 Pin
CrazyToCode20-Mar-14 23:10
CrazyToCode20-Mar-14 23:10 
Questioncomments Pin
Member 97211337-Jan-14 17:45
Member 97211337-Jan-14 17:45 
GeneralMy Vote of 5 Pin
RohitNegiRajput15-Dec-13 22:21
RohitNegiRajput15-Dec-13 22:21 
GeneralRe: My Vote of 5 Pin
Chetan Kudalkar16-Dec-13 7:04
Chetan Kudalkar16-Dec-13 7:04 
GeneralNyc explanation... Pin
Atul Khanduri5-Dec-13 20:41
Atul Khanduri5-Dec-13 20:41 
GeneralMy vote of 5 Pin
Innocent91027-Aug-13 20:45
professionalInnocent91027-Aug-13 20:45 
GeneralMy vote of 5 Pin
Amir Mohammad Nasrollahi11-Aug-13 21:41
professionalAmir Mohammad Nasrollahi11-Aug-13 21:41 
GeneralMy vote of 5 Pin
PravinSrinivasan14-Jun-13 4:16
PravinSrinivasan14-Jun-13 4:16 
QuestionComment Pin
Member 1005762927-May-13 20:59
Member 1005762927-May-13 20:59 
AnswerRe: Comment Pin
Atul Khanduri5-Dec-13 20:31
Atul Khanduri5-Dec-13 20:31 
GeneralMy vote of 4 Pin
Rockstar_12-May-13 23:26
professionalRockstar_12-May-13 23:26 
GeneralNice Pin
AVINCODE12-May-13 20:54
AVINCODE12-May-13 20:54 
GeneralMy vote of 5 Pin
Tirujit30-Apr-13 4:50
Tirujit30-Apr-13 4:50 
QuestionNice explanation Pin
Member 849336819-Apr-13 2:30
Member 849336819-Apr-13 2:30 
AnswerRe: Nice explanation Pin
Atul Khanduri5-Dec-13 20:39
Atul Khanduri5-Dec-13 20:39 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.