Difference Between Override and New Keyword in C#

This is very common question of job interview. Personally, I encounter the question few times in interview and in normal chat with tech friends. The concept is little confusing because both come in very close and situation.

Let's try to differentiate them with an example in this article. The key concept is that, both will exist when there is inheritance. We know that inheritance is a feature of OOP of which we can use the features in a class from another class without re-defining it . It's a great advantage of programming aspects. It reduces the code and time for the developer, but it can be confusing when we need to define the existing component in a new class and this is the where override and new keywords are useful.

And the general idea is, override re-defines the method that is already defined in the base class and new hides the base class method.

So, let's see in example.

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().



Now, I hope you understand both override and new qualifiers in C#. Happy learning.

Next Recommended Readings