1

Hi my workmate just told me that it is possible to achieve polymorphism by hiding a base class method and implementing another method with the same signature in the derive class, Is this true and how could this be needed in real life situations?

  • 1
    The C# Programming Guide on MSDN has a [whole section](http://msdn.microsoft.com/en-us/library/ms173152%28v=vs.100%29.aspx) on polymorphism and it's uses, including Hiding Base Class Members with New Members among other topics. – raveturned May 02 '12 at 14:49
  • I'm not sure whats unclear. Its obvious that this question is about confusion on C#s hiding "feature" and actual polymorphism, and there's a question that addresses this confusion below. – Andy May 02 '12 at 14:50

2 Answers2

2

I guess your friend was talking about shadowing. It's not true polymorphism and should be avoided Difference between shadowing and overriding in C#?

Community
  • 1
  • 1
empi
  • 15,755
  • 8
  • 62
  • 78
0

That is pretty much anti-polymorphism. The 'real' method for the object should be executed whenever called, regardless of the type of the variable, for polymorphism; you expect derived.Method() to be called whether you call it on a variable of type base or type derived, if the object in question is of type derived.

Hiding breaks that expectation.

Andrew Barber
  • 39,603
  • 20
  • 94
  • 123