Difference Between Override and New Keyword Explained Step-by-Step

this article explains a topic that is very commonly asked in interviews, and quite confusing. Let's try to clarify it. So first we will talk about "Override".

  1. Override: As we all know, Override implements Polymorphism. Which says I want to use the same method but in various classes with the same signature of methods. So let's see why we need override and how we can use it in our applications.

    Override-and-New-Keyword1.jpg

    In the snapshot above you can see I tried to use the same method in two different classes with the same signature. As you can see, the derived class's "Display()" method gave me a Warning. Let's check it.

    Override-and-New-Keyword2.jpg

    Now the compiler is confused between the two same methods. So it warns us that use can use the "New" keyword to hide the method above. We will talk later about the New Keyword, now we are talking about Override. So we will solve it using the Override keyword.

    Override-and-New-Keyword3.jpg

    Override-and-New-Keyword4.jpg

    Now you can see, I use the two keywords "Virtual" and "Override". Virtual in a base class and override in a derived class. The Virtual keyword is always used in a base class that tells the compiler that this method will be overriden in its derived class. Now try to call them:

    Override-and-New-Keyword5.jpg

    In the above snapshot you can see we are calling the methods from both classes, and let's check the output:

    Override-and-New-Keyword6.jpg

    As we can see in the above snapshot, using the override keyword we can give a different definition of the derived class's "Display()" method. For Derived class always derived class Display () method will call.

    Now a situation develops for us. Like,
    I am working on a module and using a DLL (Dynamic Linking Library) that has a method named Display () and I want to use the same method with same signature in my form.
    Then how can I use it???
    At this situation when I create the Display() method in my form then I must use the new keyword.
     
  2. New: "The new keyword hides the definition of the base class method and gives a new definition in the derived class."

    You guys have often read this definition but many readers read it and become confused. And say How???

    So let's see how

    I create an Assembly.
    I am using the example of an assembly because; it's the best way for beginners to understand because may be the creator of the Assembly doesn't want other developers to override that method. So when we need to use the new keyword and provide a new definition for that method. See the following:

    Override-and-New-Keyword7.jpg

    Override-and-New-Keyword8.jpg

    Here I created a DLL with the name TastBase that contains a class Base and a method Display. Then I built the project. My DLL is ready.
    I will now use this DLL in my form.

    Override-and-New-Keyword9.jpg

    Override-and-New-Keyword10.jpg

    Add this DLL is in the project and inherits the base class and uses the Display() Method and see what happens:

    Override-and-New-Keyword11.jpg

    Here I inherit the base class and use the same method. See, it gave me a warning. Now I will use the new keyword and try to call this method.

    Override-and-New-Keyword12.jpg

    Now anyone can guess the output:

    Quite interesting

    For most users the output should be like.

    I am Derived Class Display Method
    I am Base Class Display Method
    I am Derived Class Display Method

    But it's not like that:

    Override-and-New-Keyword13.jpg

    Rather than the derived class object, whenever you call this method it will call the Base class method. This new keyword hides the value only for that class.
    And out of this class or in another class when you call this method it will call the Base class Display Method.

Up Next
    Ebook Download
    View all
    Learn
    View all