I want to know if its possible to hide a base class property from a derived class:
Example:
class BaseDocument
{
public string DocPath{get; set;}
public string DocContent{get; set;}
}
class DerviedDocument: BaseDocument
{
//this class should not get the DocContent property
public Test()
{
DerivedDocument d = new DerivedDocument();
d.//intellisense should only show me DocPath
//I do not want this class to see the DocContent property
}
}
I cannot make the DocContent property private, because I want to instantiate the BaseDocument class elsewhere and use the property there. That will kill the idea of a property anyway.
One way to fix this would be to use a interface, say IDoc, which exposes DocPath property and make both the BaseDocument and DerivedDocument implement the interface. This will break their parent-child relationship though.
I can play with the new and override keywords, but that’s not the right way either because the child still ‘sees’ the property
I tried using the ‘sealed’ keyword on the DocContent, but that does not seem to solve the problem either.
I understand that it ‘breaks’ inheritance, but I guess this scenario should be coming up frequently where a child needs to get everything else from the parent but one or two properties.
How can such scenarios be handled gracefully?
I’m not sure inheritance would be the way to go here. Yes, you can hack around it by using the EditorBrowsableAttribute but I think the design should be rethought. One possible approach:
Basically, use composition instead of inheritance, and program to an interface, not to an implementation.