Knowledge Management Banner

Knowledge Management Banner

C# : Method Overriding , Method Hiding , Method Overloading : Part 8

 Method Overriding

When a base class reference method pointing to child class object will invoke the overridden method in child class is called Method Overriding

Overriding is when you provide a new override implementation of a method in a descendant class when that method is defined in the base class as virtual.

e.g. consider example of following console application

namespace CsharpProject
{
    public  class Class1
    {
        public virtual void PrintMessage()
        {
            Console.WriteLine("This is message is from class one.");
        }
    }

    public class Class2 : Class1
    {
        public override void  PrintMessage()
        {
            Console.WriteLine("This is message is from class Two.");
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Class2 objClass2 = new Class2();
            objClass2.PrintMessage();
        }
    }
}

In above o/p the message from overridden method is printed

Method Hiding

Hiding is when you provide a new implementation of a method in a descendant class when that method is not defined in the base class as virtual, or when your new implementation does not specify override.

e.g. consider example of following console application

namespace MethodHiding
{

    public class Class1
    {
        public void PrintMessage()
        {
            Console.WriteLine("This is message is from class one.");
        }
    }

    public class Class2 : Class1
    {
        public new void PrintMessage()
        {
            Console.WriteLine("This is message is from class Two.");
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Class2 objClass2 = new Class2();
            objClass2.PrintMessage();
        }
    }
}

The output is printed by the new method from second class

Method Overloading

When multiple method with same name and different signature are implemented in a class is called method overloading.

e.g. consider following example of console application

namespace MethodOverloading
{
    public class ClassOne
    {
        public void PrintResult(int a, int b)
        {
            Console.WriteLine("The result is {0}", a + b);
        }

        public void PrintResult(int a, int b,int c)
        {
            Console.WriteLine("The result is {0}", a + b + c);
        }

    }

    class Program
    {
        static void Main(string[] args)
        {
            ClassOne objClassOne = new ClassOne();
            objClassOne.PrintResult(2, 3);
            objClassOne.PrintResult(4, 7, 9);
        }
    }
}

In above example the method to be invoked is done during runtime, thus it has same name but different signature ( different parameters passed )


Method overriding vs Method Hiding

Actual difference between overriding and hiding is as follows:
  • If a method is overridden, the implementation to call is based on the run-time type of the argument this.
  • If a method is simply hidden, the implementation to call is based on the compile-time type of the argument this.





No comments

Powered by Blogger.