How can I hide the parent class property in child class.
Where parent class has a property called "Parent", where I don't want to use that in child class. How can I remove or hide that.
How can I hide the parent class property in child class.
Where parent class has a property called "Parent", where I don't want to use that in child class. How can I remove or hide that.
Don't.
You have a design issue if you need this. The Liskov Substitution Principle tells you that your
Child
class should be substitutable for aParent
class. That means that all code which uses aParent
class should be able to use yourChild
class instead. This is not the case if you would remove a property. You couldn't replace a Parent with a Child wherever that particular property is used.If you derived class when you say child class there's luckily no way to make it go a way. To why that would be bad tale a look at this code:
base myObj = objectFactory.Create(); myObj.theMethodHiddenAway();
the factory Can return any object of type base including all classes that derive from base. The compiler needs to decide what version of theMethodHiddenAway to call compile time (overload resulution) and since it does not no the concrete type compile time, if it was possible to make a method go away by inheritance the compiler wouldn't know if theMethodHiddenAway even existed.
I'd suggest you look at your design since what you're asking smells like one of two design flaws. Either the method does not belong there in the first place or you derived class shouldnt inherit from the base class
Hope this help, you can shadow the property and set it readonly :)
I guess the obvious question/answer is, maybe you should just make the property private. (unless of course you have no access to the source to begin with)
So it sounds like you are asking the following. You have
and you want to hide
SomeProperty
from being visible in instances ofChild
.Do not do this! Do not hide properties from the base class that are visible. First, it's easy to get around:
Second, it's a huge violation of the Liskov substitution principle. Basically, the principle says that anything that you know to be true about all instances of
Parent
should also be true about all instances ofChild
. Here, we know that all instances ofParent
have a visible property calledParentProperty
of typeSomeType
. Therefore, the same should (moral should) be true of all instances ofChild
.Can you tell us why you want to do this and maybe we can suggest an alternative?
You can't. A child class reference will always be implicitly convertable to a parent class reference, so you can't prevent the property from being used wihtout removing it from the parent class.