tags:

views:

56

answers:

2

Maybe I'm thinking about this the wrong way but here's the idea:

Class A gets, as part of it's constructor, a pointer to class B and saves that pointer in a private variable. Class B exposes a public function, F. I'd like for class A and all classes that inherit from class A to NOT be able to call B.F.

The idea is that class A will implement its own version of F, one that calls B.F, but the rest of the code should not be calling B.F. If it matters, the two have different function signatures.

Can this be done? Or should it be done? Maybe another way, like a class C that inherits from class B and hides the public functions? (Cant that even be done?)

+3  A: 

You shouldn't do this. It violates the Liskov Substitution Principal.

If you really want to hide base functions, the "A" class shouldn't be a base class, but rather a separate class you encapsulate in B and C.

Reed Copsey
+2  A: 

This is not possible. If the method F on type B is public and a type otherwise has access to B, there is no way to prevent the type from calling into the public method. There are lots of tricks you can play to make it difficult with shadowing methods (new in C#) but at the end of the day a cast back to B is all that's need to override your mechanisms.

If you truly want to hide everything then create a new interface which only exposes the subset of methods you want available. Then pass an instance of that to the type.

JaredPar