ARTICLE

Method Overriding and Method Hiding in C#

Posted by Shees Abidi Articles | C# Language February 13, 2009
In this article, I attempt to clarify the notions of frequently used terms in C# like method overriding and method hiding. I hope you find my article simple, lucid and informative.
Reader Level:

One of the most important oops concepts in .NET is Abstraction that literally means 'act of representing only essential features without including background details or explanations.' C# conforms well to this concept and provides various useful ways to implement this concept.

Note:This article may be considered as a foundation for the next article to be published regarding 'Abstract Classes and Interfaces in C# and differences with Overriding and Hiding

Lets first understand the use of 'virtual' keyword:

Method Overriding:

In some situations we want to have a class that has types(specially methods) that can possibly be modified in the derived class. In such situation we declare the method in the base class as virtual and precede the same method in the derived class with 'override' keyword

Consider the following code in which situation is that, a base class method Area() returns square of a parameter and suppose we want to extend this method in the derived class sothat this square may just be multiplied by a constant pi(3.142) so that it can return area of a circle:

using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication1
{
      public class VirtualDemo
      {
            public virtual double Area(double r)
            {
               return r * r;
            }
      }
      public class A : VirtualDemo
      {
            public override double Area(double r)
            {
               double p = 3.142;
               return base.Area(r) * p;
            }
      }
      public class Test
      {
            public static void Main(string[] args)
            {
               A obj1 = new A();
               Console.WriteLine(obj1.Area(3));
               Console.ReadLine();
            }
      }
}

Output:

28.278
KeyPoints:

~virtual method must have its implementation in the base class and may optionally be overrided in the derived class if some additional functionality is required

~signature of the method in base as well as derived class should be the same

Now lets understand Method hiding by slightly modifying the same code:

Method Hiding:

In some situation we may want to have a method in the base class which can be implemented in the derived class independent of the method in the base class i.e we may want to have its new version altogether

In the following code i add a method Hello() in the base class and in the derived class give a completely new definition to the same method by preceding it with 'new' keyword

using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication1
{
           public
class VirtualDemo
            {
                  public virtual double Area(double r)
                  {
                        return r * r;
                  }
            
      public void Hello()
                  {
                        Console.WriteLine("Hello in Base Class");
                  }
            }
         public class A : VirtualDemo
         {
               public override double Area(double r)
               {
                  double p = 3.142;
                  return base.Area(r) * p;
               }
               
public new void Hello()
               {
                  Console.WriteLine("Hello in Derived Class");
               }
         }
         public class Test
          {
            public static void Main(string[] args)
            {
                  A obj1 = new A();
                  Console.WriteLine(obj1.Area(3));
                  obj1.Hello();
                  Console.ReadLine();
            }
         }
}

Output:

28.278
Hello in Derived Class

KeyPoints:

~if any class method doesnot have 'virtual','abstract' or 'override' keyword and we attempt to have its new implementation in any derived class a warning is generated to supply 'new' keyword to the method in the derived class.We can also not override that class method unless that method has 'virtual' keyword

That was in brief about Method Overriding. In case of any query ask me on the same blog. In my next article i will explain Abstract Classes and Interfaces with examples and how these all concepts relate to each other and differ from each other with similar and even better approach.

Ask me if you have any query so far after reading the article.

Login to add your contents and source code to this article
post comment
     

nice

Posted by Pramod Lovete Oct 30, 2012

thanks

Posted by j dawg Jun 04, 2010

Hi,

Want to know what is the advantage of overriding,From the above article says "Method in base class is overrided(modified) a method in derived class i,e area of  square(method)  in base class is overrided for area of circle in derived class"

from the above context what is the need of overriding.I can declare two classes one for Square area and one for Circle area.

Posted by Gowrikanth vISWANADHA Dec 06, 2009

hi....
Greetings. i read your article about Overriding Vs Hiding. Here you define difference between Overriding Vs Hiding in terms of declaration and use. i want to know the difference in terms of memory usage  and why we need to use new key word if we do can the same with Overriding and virtual keyword.

Posted by Pankaj Yadav Sep 02, 2009

Hello Shees,
Thanks a lot for posting a nice article. It helped me a lot to understand the basic ofmethod overriding and method hiding in c#. There are some tricky points which are actually very essential to know for every developers. No more today. Keep going on.---
Golam Rabbi


Posted by Golam Rabbi Jul 24, 2009
COMMENT USING
PREMIUM SPONSORS
Over-C is a holistic consortium of communications and technology specialists. We build, deploy and market both business as well as consumer products and solutions.
Join a Chapter
SPONSORED BY
  • PDF reports have never been easier to create. With our included WYSIWYG Designer, you can layout your reports, set up your data source and let DynamicPDF ReportWriter do the rest.