In C++ it is done with “using”, and in C#?
public class foo
{
public void print(string s) {...}
}
public class bar : foo
{
// shadowing
public void print(object o) {...}
}
How to promote foo.print, so foo.print and bar.print would be at the same “level” for compiler (for bar of course)?
Update 1
Originally I added a paragraph about common confusion between shadowing and overriding, but then I deleted it, because I thought it will be offensive to readers.
Shadowing is like overloading spanned over inheritance tree. Shadowing is NOT overriding.
Update 2
After shadowing foo.print is no longer taken into account when resolving the overloaded method print. Promoting foo.print would get it back into process — so when I call bar_object.print("hello") the method foo.print would be called.
In your concrete example,
bar.print(object)indeed “shadows” the more specificfoo.print(string):This will call the method defined on
bar, although the method onfoowould have a parameter that matches the type better.What happens here is the following: The compiler finds a method on
barwith the right name (“print”), the right number of parameters (1) and a parameter type to which the passed in parameter is convertable to (stringcan be converted toobject).Because of this, there is no reason for the compiler to look further up the inheritance chain.
As far as I am aware, there is no construct similar to C++’s
using.If you want to use the method defined on the base class, you basically have three options:
On the caller side: Convert the
barinstance tofoo:On the calee side: Inside
bar.print(object)check the type of the passed parameter:This will come the closest to the C++
using: Actually override or hide the original method in the derived class: