0

I have a problem with terminology. According to MSDN: "The override modifier extends the base class method, and the new modifier hides it." However, in the following example:

using System;

namespace ConsoleApplication2
{
    class BaseClass
    {
        public virtual void Method1()
        {
            Console.WriteLine("Base - Method1");
        }
    }

    class DerivedClass : BaseClass
    {
        public void Method1() // DerivedClass.Method1() hides inherited memeber BaseClass.Method1(). Use the new keyword if hiding was intended.
        {
            Console.WriteLine("Derived - Method1");
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            BaseClass bd = new DerivedClass();
            bd.Method1();
            Console.ReadLine();

        }
    }
}

You will see that if you use new in declaring Method1() in DerivedClass, bd.Method1() will output: "Base - Method1" as instructed in the base class.

...whereas, if you use override in declaring Method1() in DerivedClass, bd.Method1() will output: "Derived - Method1" as instructed in the derived class.

Why does every source (including the official documentation) say that new hides the base class method, when clearly in this example the base class method is the one invoked when using new?

I understand the different behaviours (new compared to override) but not the terminology.

Sami
  • 393
  • 8
  • 22
  • check this, https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/knowing-when-to-use-override-and-new-keywords, for `hiding`, `new` should be there in the method definition. – Bharadwaj Jan 29 '18 at 12:06
  • 2
    Possible duplicate of [What is the difference between the override and new keywords in C#?](https://stackoverflow.com/questions/6576206/what-is-the-difference-between-the-override-and-new-keywords-in-c) – Guy Jan 29 '18 at 12:10
  • It hides it in the DerivedClass (and any descendants of it). It doesn't hide it in BaseClass. So yes, it does really hide the base class method, but that does not mean what you obviously think it does. See my answer too. – Rudy Velthuis Jan 29 '18 at 12:34
  • `new` does nothing to your output here. It only suppresses a warning. It's just terminology, in VB the equivalent keyword is `Hides`. – H H Oct 09 '18 at 16:48

2 Answers2

1

There are several uses of new. The one mentioned in the docs you refer to is the declaration modifier.

Your Method1() in the derived class can be declared in three ways:

public void Method1()         // 1
public new void Method1()     // 2
public virtual void Method1() // 3

Both version 1 and 2 hide the virtual method of the same name in the base class, so when called on a base class variable, BaseClass.Method1() will be called. But version 2 (with new) explicitly hides the base class method, i.e. you are telling the compiler that this was intentional. Version 1 implicitly hides the virtual method and will result in a warning, because the compiler assumes it could be accidental, and not intentional.

Hiding does not mean what you think it does. It means that in DerivedClass and any descendants of it, Method1() is not virtual and that you can't call the virtual method on a DerivedClass anymore. They say that the virtual method is hidden from DerivedClass. That means it breaks (inheritance) polymorphism.

Version 3 overrides the method of the same name, and if a method is virtual, the actual method will be called, no matter how the variable is declared. So if the variable is declared as BaseClass, but in reality the class is a DerivedClass, then the derivate's method will be called.

More info:

https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/new-modifier

Note that that use of new is not the same as what you understood:

BaseClass bd = new DerivedClass();

That is a different new, and not the declaration modifier.

Rudy Velthuis
  • 28,387
  • 5
  • 46
  • 94
1

What does it mean that the method hides the method from the base class?

class BaseClass
{
    public virtual void Method1()
    {
        Console.WriteLine("Base - Method1");
    }
}

class DerivedClass : BaseClass
{
    public void Method1() // DerivedClass.Method1() hides inherited memeber BaseClass.Method1(). Use the new keyword if hiding was intended.
    {
        Console.WriteLine("Derived - Method1");
    }
}

Imagine if you've written the following:

var dc = new DerivedClass();
dc.Method1();

You compile and run this code, and the derived method is called. But now, delete Method1 in BaseClass. The code still compiles and runs exactly the same.

Method1 in DerivedClass hid the Method1 method in BaseClass from anyone using DerivedClass directly (or any further derived instances)

Damien_The_Unbeliever
  • 234,701
  • 27
  • 340
  • 448