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.

Employers - Post Free Jobs