Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What scenarios does it make sense to use "method hiding"? [duplicate]

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

like image 729
siamak Avatar asked Jan 22 '13 15:01

siamak


People also ask

Why do we use method hiding?

It tells us to use the new keyword to hide the inherited member. So, by using the new modifier in the derived class method, it hides the implementation of the base class method. This is called Method Hiding. It allows you to provide a new implementation for a derived class.

What is the need of method hiding in Java?

Method hiding means subclass has defined a class method with the same signature as a class method in the superclass. In that case the method of superclass is hidden by the subclass. It signifies that : The version of a method that is executed will NOT be determined by the object that is used to invoke it.

What is need of method hiding in C#?

C# also provides a concept to hide the methods of the base class from derived class, this concept is known as Method Hiding. It is also known as Method Shadowing. In method hiding, you can hide the implementation of the methods of a base class from the derived class using the new keyword.

What is the difference between method hiding and overriding in C#?

Hiding redefines the complete method, whereas overriding redefines only the implementation of the method. In Overriding, you can access the base class using the child class' object overridden method.. Shadowing has cannot access the child class methods.


1 Answers

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.
like image 52
cdhowie Avatar answered Sep 20 '22 17:09

cdhowie