Sign in to follow this  
phb5000

[.net] C# Protected Inheritance Access

Recommended Posts

phb5000    122
To make my question clear I'm going to start by setting up an example: - I have an abstract class with a certain Property - A Derived class should be able to choose whether, or not, to allow the property to be accessible to everyone using this derived class. abstract class Class1 { protected virtual int TheProperty { base set... base get... } ... } class DerivingClass : Class1 { // The override may be used like this protected override int TheProperty { base set... base get... } // Or this may happen... protected override int TheProperty { something else set... something else get... } } Currently I solved this problem by making The Property protected and virtual in the abstract class. This way, the deriving class will have access to The Property, and IF it wants to, it can override it so that it can be accessible to everyone. However, this doesnt seem very robust since there is a risk of changing the way the property is handled in the derived class. I DO NOT want the property itself to be changed, I just want to change the way the access is "passed along". I hope this is a good enough explanation. Thank you,

Share this post


Link to post
Share on other sites
Bob Janova    769
Can't be done. The whole point of inheritance and overrides is that while the interface to the class is maintained, you can change the implementation.

Why does it matter that it is impossible to change the implementation? You can always put in your documentation that it usually isn't necessary, but it's best to let the author of that override code decide what is best.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this