I'm extending a libary class which has a public Method that I don't wan't to be callable from the outside. I know that I can hide it using the "new" keyword, but the problem is, that it has a lot of different declarations. Something like this:
class Parent() {
public double method(double a, double b) { ... }
public int method(int a, int b) { ... }
public System.Int32 method(System.Int32 a, System.Int32 b) { ... }
}
Now I could do:
class Child() {
// Make method private.
private new double method(double a, double b) { return base.method(a, b) }
private new int method(int a, int b) { return base.method(a, b) }
private new System.Int32 method(System.Int32 a, System.Int32 b) { return base.method(a, b) }
/// Own Code.
}
But I'm wondering if there is not a simpler way to make all overrides of that function private.
That's not the way method hiding works. Your private new
methods only hide the original methods inside the Child
class (but they call what they hide, so nothing's gained).
In a Grandchild
class deriving from Child
, nothing is hidden anymore because the private new
methods are not in scope, so the public
methods of Parent
are perfectly callable and "naked". Method hiding does not remove the hidden method in any way; it's just a matter of which of the identical methods gets called.