I do not think this is possible to do, but I am curious. Is it possible to make a protected variable in a super class private in its subclass.
For example, I have three classes SuperClass, SubClass : SuperClass and SubCLass2 : SubClass.
I want to be able to access variable protected string[] commands that is in SuperClass from SubClass but prevent access to it from SubClass2.
If this is not possible, is there anyway to achieve the same effect?
No. Personally, I would avoid having the variable non-private in the first place – I’d provide a protected property. You could create a new property to “hide” the base property… but this smacks of poor design to me.
I can’t think of any way of getting around the hiding from the grand-child’s point of view, without changing one of the other classes, but it feels pretty arbitrary to me:
Why do you want to achieve this? Perhaps you should seal your direct subclass, and force other classes to use composition instead…
EDIT: As explained in the comments, it sounds like this is a situation where making the property (and I would still make it a property returning a private field) internal would be appropriate. That gives access to the property to all other code in the same assembly, but no other assemblies.