I had created a base class that has many public properties and were been used perfectly. Now i want to use this class to derive other class , but i do'nt want some of its properties to be exposed outside the derived class that inherits it. Is there any way that the properties of base class that are public cannot be exposed outside its derived class.(The properties that are to be hidden are public because they are used in other classes that inherits it).Any help will be highly appericiated.
-
6http://en.wikipedia.org/wiki/Liskov_substitution_principle – Kirk Woll Mar 01 '12 at 18:49
3 Answers
You want to make them protected.
From MSDN:
A protected member is accessible within its class and by derived class instances.

- 17,109
- 5
- 51
- 69
-
+1, but for clarity, you might want to add that they would need to be protected *in the base class.* He might take this to mean he can change their visibility in the derived class. – Anthony Pegram Mar 01 '12 at 18:52
-
looks like he wants public properties still in the base class but not in the derived class, so this goes against Liskov principles as shown in the comments of the original question. – bdparrish Mar 01 '12 at 18:54
-
@bdparrish That is what I thought until I read the second to last sentence. "The properties that are to be hidden are public because they are used in other classes that inherits it" – cadrell0 Mar 01 '12 at 18:57
-
i cnt make them protected because they been used previously and works perfectly and can be exposed outside the derived class. but now am deriving a new class that requires almost same properties and methods except two properties. – yo chauhan Mar 01 '12 at 19:01
-
1
-
@cadrell0, right so protected means that only B can access x, but in this case the original question asks to be able to change the access flag to not be public meaning hide it in B, but still show it in A. – bdparrish Mar 01 '12 at 19:04
I agree with cadrell0 about marking them protected, but just in case you are looking for a solution where the properties are actually public
, but hidden to users of a certain derived class, you can use an explicit interface
interface IHaveMethodsYouCanHide { void Foo(); }
class Base : IHaveMethodsYouCanHide { public void Foo() {} }
class NonHidingDerived : Base { }
class HidingDerived : Base, IHaveMethodsYouCanHide
{
void IHaveMethodsYouCanHide.Foo() {}
}
With this code, identifers of type HidingDerived will not allow calls to Foo (unless first cast to IHaveMethodsYouCanHide).

- 4,501
- 1
- 24
- 32
What you're asking for is simply not possible. If type B inherits from type A then it "is-a" type A. B has at least the same accessible contract that type A had. There is no way to hide a public member of A without fundamentally violating this contract.
If you find yourself in a scenario where you want to use A but only expose a subset of the properties then inheritance is not the right solution: containment is the proper solution.
public class B {
private A m_wrapped;
// Expose only the properties you want to expose here
}

- 733,204
- 149
- 1,241
- 1,454
-
You actually can hide members of base types in derived types, read my answer with explicit interface implementations – payo Mar 01 '12 at 20:06
-
@payo that doesn't change the visibility of existing members. They are still accessible when an instance of the derived is visible from a reference to the baes. – JaredPar Mar 01 '12 at 20:08
-
It does change their visibility as seen from usage of the derived class (which you also refer by constraining that an identifier be of the base type). I find it important to make this distinction because the original question _might be_ asking how to hide and my answer simply shows the one solution where hiding is possible. – payo Mar 01 '12 at 20:17