views:

293

answers:

6

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.

+4  A: 

So it sounds like you are asking the following. You have

class Parent {
    public SomeType ParentProperty { get; set; }
}

class Child : Parent { }

and you want to hide SomeProperty from being visible in instances of Child.

Do not do this! Do not hide properties from the base class that are visible. First, it's easy to get around:

Parent p = new Child();
p.ParentProperty; // oops!

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 of Child. Here, we know that all instances of Parent have a visible property called ParentProperty of type SomeType. Therefore, the same should (moral should) be true of all instances of Child.

Can you tell us why you want to do this and maybe we can suggest an alternative?

Jason
Note different casing is recommended for private members.
Dykam
but it was public in parent class
santose
@santose: Can you clarify if you're talking about nested classes or derived classes?
Jason
It was a derived class
santose
@santose: Okay, please see my accordingly-edited answer.
Jason
A: 

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

Rune FS
A: 

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.

Joe Gauterin
+1  A: 

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 a Parent class. That means that all code which uses a Parent class should be able to use your Child 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.

MSalters
+2  A: 

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)

aggietech
A: 

Hope this help, you can shadow the property and set it readonly :)

Angel Arguijo