Today I had an interview where I have been asked
Is it possible to perform Method Hiding without using New keyword?
As far as I know , it is not possible. So I said that.
Is there any (seriously) tricky / alternate way to perform Method Hiding? I thought of Sealed once....
Also like Explicit Interface, is there something of that sort...(not sure)
Edited
As a last question on this topic, like we can do for explicit interface
Interfacename.Method name
Can we do something similar for a derived class like.
Class Derived:Base
{
public void Base.SomeMethod(){}
}
Absolutely - if you don't specify anything, you still get the same effect as with the new
modifier, but you get a warning as well.
There's also explicit interface implementation, of course.
In both cases, just casting the target to a different type "unhides" the method:
((BaseClass) x).SomeMethod();
((IFoo) x).SomeMethod();
Sealing a class has nothing to do with method hiding though - or rather, it prevents any derived classes from hiding a method, by making sure there are no derived classes.
See more on this question at Stackoverflow