Saturday, April 18, 2020

Override vs New in C#

Override qualifier to re-define base classes function

When we want to re-define a method in a derived class we can override the method in a derived class and for that the base class function must be virtual in nature. Then the compiler will know that Oh, the method might be re-defined somewhere in a future class implementation. The override keyword might come before or after the qualifier. Here is a sample implementation.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleAPP
{
    public class baseclass
    {
        //Implementation of base hell function
        public virtual void hello()
        {
            Console.WriteLine("I am base hello");
        }
    }
    
    public class deriveclass :baseclass
    {
        //Implementation of derive hello function
        public override void hello()
        {
            Console.WriteLine("I am derive hello");
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            baseclass objb = new baseclass();
            deriveclass objd = new deriveclass();
            baseclass bc = new deriveclass();

            objb.hello(); //base hello
            objd.hello(); //derive hello
            bc.hello(); //derive hello

            Console.ReadLine();
        }
    }
}

Let's explain the output a little bit. Please look at the definition of the Main function. The first object is created for the base class, the second for the derived class and the third for again the derived class and for the base class both. Yes, in confusion you can check with the “is” operator. And the output is here.



New qualifier to hide base class function
Yes, but if we do not hide using the new qualifier then the compiler will provide a polite warning and it will be hidden explicitly from it's own. Let's see the following code.



The signature of the hello() function is the same in both classes and one is inherited from the other and we see the polite warning from the compiler. So, the new qualifier will hide the base implementation in the derived class. Now, let's use the “new” qualifier in a derived class's function definition. Here is sample code.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleAPP
{
    public class baseclass
    {
        //Implementation of base hell function
        public void hello()
        {
            Console.WriteLine("I am base hello");
        }
    }
    
    public class deriveclass :baseclass
    {
        //hide hello function from it's base implementation
        public new void hello()
        {
            Console.WriteLine("I am derive hello: ");
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            baseclass objb = new baseclass();
            deriveclass objd = new deriveclass();
            baseclass bc = new deriveclass();

            objb.hello(); //base hello
            objd.hello(); //derive hello
            bc.hello(); //base hello

            Console.ReadLine();
        }
    }
}

Again, like the override keyword , new could be after or before the visibility scope qualifier and if we compare the current output with the previous one, we will see the small change in the third function call. In case of overriding it was invoking the derived hello() whereas now it's invoking the base hello().


No comments:

Post a Comment