9

Possible Duplicate:
method hiding in c# with a valid example .why is it implemented in the framework.? what is the Real world advantage.?

I am wondering how method hiding could be useful in c# ? If you dive into the concept ,you can find that method hiding is something confusing and drives us to spaghetti code style.There are some reasons for that :

1- If you need a fresh method in your Derived class you can define a new method with a new name in your derived class.

2-In a polymorphic manner usage when you use upcasting, the expected behavior would not be achieved and this could be confusing.

(Specially this strange behavior could be so apparent and obvious if the Base class and the Derived Class were in the different libraries and you want to have a polymorphic usage of them in somewhere in your code)

class Base
{  
    public void Method() { Console.WriteLine("I am Base Class"); }
}
class Derived: Base
{
    public new void Method() { Console.WriteLine("I am Derived Class");}
}
class Program
{
    static void Main(string[] args)
    {
        Base C = new Derived();
        C.Method(); //output:"I am Base Class"
    }
}

Such a behavior could be confusing, but If we used "virtual" in "Base" and "override" in "Derived" (I mean a polymorphic usage) the output was: "I am Derived Class". and this output is what that I think the most of us expected.

In other words I believe that in a hierarchy, most of developers expected for overridden methods in a derived class Not New methods that wanna hide the base class implementation.

I cant understand why such a bad thing that has no advantages there is in c# language? at least I want to know what is the best usages of method hiding in c# ?

thank you for your help

Community
  • 1
  • 1
siamak
  • 669
  • 1
  • 10
  • 28
  • 1
    *"the expected behavior would not be achieved"* - This depends on what the developer is expecting. If he's expecting non-virtual methods to behave virtually, that's a flawed expectation. – cdhowie Jan 22 '13 at 15:12
  • 3
    Here's an example: If you have a collection of Animals, and iterate through the collection to call Speak() on each Animal, wouldn't you want Cows to say "Moo" and Dogs to say "Woof"? – mbeckish Jan 22 '13 at 15:14
  • 1
    @mbeckish That's a contrived case where you *do* want virtual methods. One single counter-example doesn't negate an entire feature. – cdhowie Jan 22 '13 at 15:16
  • I'm not sure you're using the term "method hiding" correctly. Your code sample is an example of method hiding. If the method were virtual, it would be method _overriding_. Which one are you saying is not a good thing? – JLRishe Jan 22 '13 at 15:19
  • This is a duplicate of http://stackoverflow.com/questions/2663274/is-method-hiding-ever-a-good-idea and http://programmers.stackexchange.com/questions/153738/what-are-some-practical-uses-of-the-new-modifier-in-c-with-respect-to-hiding. See also my article http://blogs.msdn.com/b/ericlippert/archive/2008/05/21/method-hiding-apologia.aspx and – Eric Lippert Jan 22 '13 at 15:21
  • I cant personally accept method hiding as a strategy for developing code. In my opinion, if you feel the need to hide the method on the base class then you are most likely doing something wrong. I haven’t come across any scenarios where method hiding couldn’t be better implemented by other means, even as simple as just naming the method on the derived class to something else. – siamak Jan 22 '13 at 15:25
  • @cdhowie - How was my example negating a feature? As you said, my example was a case where you *do* want virtual methods. – mbeckish Jan 22 '13 at 15:49
  • @mbeckish This question is about when method hiding is useful, not when virtual methods are useful. – cdhowie Jan 22 '13 at 15:55
  • @cdhowie - The OP said the case where the base class's method is virtual and the derived class's method gets called, resulting in "I am Derived Class", is the confusing behavior. – mbeckish Jan 22 '13 at 16:00

1 Answers1

7

If you need a fresh method in your Derived class you can define a new method with a new name in your derived class

Not always. Consider the case where you derive a class that is exposed in some other library, one that you do not have control over. You add some methods to your class.

Later, the developer of the library you depend on adds some enhancements and uses the same name as one of the methods you added to your derived class. Hiding in this case is merely the preservation of existing functionality. If you change your method name, you have to update your entire code base, and you break compatibility with anyone who is using your library. Instead, you can hide your existing method and retain compatibility with your dependents and with the library you depend on at the same time.

Personally, the case where I use method hiding the most is when implementing a clone operation on multiple classes; without this feature, I could not change the return type to match that of the class:

class A
{
    public A Clone()
    {
        return CloneImpl();
    }

    protected virtual A CloneImpl()
    {
        return MemberwiseClone();
    }
}

class B : A
{
    new public B Clone()
    {
        return (B)CloneImpl();
    }
}

The protected virtual method means that no matter which Clone() you call, you are still getting the correct type of object back. However, the type of reference you get will depend on which reference type you used to invoke Clone(). This allows:

B b = new B();
B b2 = b.Clone(); // Did not have to cast, thanks to the hiding method.

A a = b;
A a2 = a.Clone(); // Still returns a B, only typed as an A.
cdhowie
  • 158,093
  • 24
  • 286
  • 300
  • 1
    Ah the brittle base class example. This is one reason why it's best to use composition over inheritance, when possible. – Servy Jan 22 '13 at 15:17
  • 1
    @Servy I agree. I wish C# provided a bit more in that regard; it's a pain to implement two interfaces and then delegate those to member objects. Writing the boilerplate code is not a fun exercise. (Referring specifically to the case where you want to expose the composed objects as "one" -- I forget the name of that pattern at the moment.) – cdhowie Jan 22 '13 at 15:21
  • unfortunately the question is closed but just out of curiosity even in this case if the first developer used "virtual" in the base class you could "override" the method in your derived class.I mean even in this case "overriding" is better , if in c# the definition of methods by default was "virtual" , there were no needs to hide methods.am i right? – siamak Jan 22 '13 at 15:38
  • 1
    @siamak No. If methods were virtual/override by default (think Java) then the addition of a new method in a parent class could cause methods in a derived class to suddenly override it, which could be unwanted behavior and may have bad consequences. – cdhowie Jan 22 '13 at 15:57