-1

This is Micosoft's definition for the new keyword: "The warning says that the Method2 method in DerivedClass hides the Method2 method in BaseClass." (https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/knowing-when-to-use-override-and-new-keywords)

I just can't seem to wrap my head around why it is defined as "hiding the implementation of the base class". Why does it use base class' implementation instead of derived class' implementation, if it actually "hides" the base class' implementation? It just seems to me the use of word "hide" contradicts to how it actually works, and that explanation leans toward what keyword override is actually doing; using the derived class' implementation instead of base class' implementation.

I would appreciate any answer that clears up my confusion on this "base class implementation hiding" by using the new keyword. Thanks all!

97nico
  • 11
  • 2
  • 1
    "I understand how it works." Sorry, but obviously you do not. `new` creates a member with the exact same name that has nothing to do with the hidden member. However you can´t access the hidden member, unless you cast that instance to the base-class. Actually you have two members with the exact same name, so you have to indicate in some way which one you want to use. – MakePeaceGreatAgain Mar 15 '19 at 21:07
  • Okay, I apologize that I claimed to have understood the concept. My confusion seems to be Base base = derived, base has virtual Foo(), and derived has new Foo(). Why base.Foo() uses base's Foo() if the keyword "new" is hiding base's Foo(). – 97nico Mar 15 '19 at 21:18
  • it's hiding child class implementation from base, so Base does not know abou `Foo` impelemented by derived – Ehsan Sajjad Mar 15 '19 at 21:24

2 Answers2

1

Why does it use base class' implementation instead of derived class' implementation

With 'new', when called on a base class variable, it will call the base class's implementation. When called on a derived class variable, it will call the derived class's implementation.

Derived d = new Derived();
Base b = d;

d.Foo(); //<- Derived's implementation
b.Foo(); //<- Base's implementation

With 'override', the derived class's implementation is called in both cases.

Derived d = new Derived();
Base b = d;

d.Foo(); //<- Derived's implementation
b.Foo(); //<- Derived's implementation
David Browne - Microsoft
  • 80,331
  • 6
  • 39
  • 67
  • Yes, I understand that with "new", it will call base class' implementation instead of derived's. But why is it said to be "hiding" the "base class' implementation"? The definition leads me to think that because b's Foo() is hidden, "Base b = d; b.Foo();" would call d's Foo() instead of b's Foo(), which resembles what keyword "override" is actually doing – 97nico Mar 15 '19 at 21:08
  • It's figurative language. "hiding" in the sense of "placed in front of", but not "replacing". – David Browne - Microsoft Mar 15 '19 at 21:08
  • Ah, "placed in front of" seems to be the answer I'm looking for. Just to clarify, am I correct to assume that "hiding" means that having keyword "new" for d's Foo() places b's Foo() in front of d's Foo()? So, if Base b = d, calling b.Foo() will run b's Foo() even though d also has an implementation for Foo(), because b's Foo() is placed in front of d's Foo()? – 97nico Mar 15 '19 at 21:12
  • @97nico I believe you have the right conclusion, but the wrong logic. `d` is in front of `b`. Its just when you have a `b` reference you go past `d` – BradleyDotNET Mar 15 '19 at 21:19
  • So, am I right this time to think that b.Foo() will run B's Foo() instead of D's Foo(), because the variable is of type B, and D's Foo() is thought to be a new and entirely unrelated function than B's Foo() because of the "new" keyword? – 97nico Mar 15 '19 at 21:28
  • @97nico Sure, that's reasonable – BradleyDotNET Mar 15 '19 at 21:31
0

I would think about it this way:

When Derived hides a member of base class Base, and you access through an Derived, Deriveds is called because it is hiding Bases implementation. However, when going through Base there is nothing hiding it, so Bases implementation is used.

However, with virtual/override (polymorphism) Bases method is actually overridden, not hidden, so it will use Deriveds method even if accessed through a Base reference.

Also (and probably the real reason): Naming things is hard. Programmers are just as bad at it as everyone else.

BradleyDotNET
  • 60,462
  • 10
  • 96
  • 117
  • That's what I thought, "A's is called because it is hiding B's implementation". But, as per Microsoft's definition, the "new" keyword "hides the base class implementation", which in this case, would re-phrase your statement to "A's is called because it is hiding A's implementation". – 97nico Mar 15 '19 at 20:56
  • @97nico In my case `A` is the derived class. Also, A cannot hide its own implementation. Not sure what your comment means – BradleyDotNET Mar 15 '19 at 20:58
  • I apologize if my comment confused you. I got confused as to which of A or B is the base class and derrived class. If A's (derrived class) function() is declared as "new", and B's (base class) function() is declared "virtual", calling A.function() will run B's function() instead, am I right? – 97nico Mar 15 '19 at 21:06
  • @97nico No. `virtual` just means it *can* be overriden. The `new` would still do standard method hiding. – BradleyDotNET Mar 15 '19 at 21:17
  • I'm really sorry, I have a trouble expressing my thoughts clearly. If B base = derived; base.function() will run B's function, right? – 97nico Mar 15 '19 at 21:22
  • @97nico yes because no override in derived class – Ehsan Sajjad Mar 15 '19 at 21:27