Re: Inheritance question
- From: "Tobin Harris" <tobin@xxxxxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Wed, 22 Jun 2005 06:58:24 +0100
As far as I know, there's no way of "hiding" inherited properies by changing
their visibility. To benefit most from polymorphism, it should always be
possible for clients to use a derived subclass as if it were a base class
without knowing it. Changing visibility in this way would break that -
suddenly your subclass does not *really* expose the interface of your base
class anymore. The Liskov Substitution Principle explains why here:
http://www.objectmentor.com/resources/articles/lsp.pdf
One option you do have is to make the properties of the base class
protected, and then re-publish only the ones you need as public in the
subclass under a different name. For example:
class myBase{
protected string name{ get{ return _name;}}
}
class mySubclass : myBase{
public string Name{ get{ return base.name; }}
}
What's your scenario? I've wished for this ability too, in the case where I
use a code generator to create a base class that I then want to extend in a
user-edited subclass. I've often realised it would be more convenient to
hide a few methods than to have to re-publish most of them as public. In my
case, the *real* problem here is that I really only need *one* class, but
instead I'm using 2 classes and inheritance to keep the generated code
separate from the hand-modified.
Hope this helps
Tobin
"Michael A. Covington" <look@xxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:uilXqiudFHA.892@xxxxxxxxxxxxxxxxxxxxxxx
> Suppose class X has some public properties, and class Y inherits from X.
> Is there a way that some of the public properties of X can be private
> (hidden) in Y (but still usable by the methods inherited from X that use
> them)?
>
>
.
- Follow-Ups:
- Re: Inheritance question
- From: Michael A. Covington
- Re: Inheritance question
- References:
- Inheritance question
- From: Michael A. Covington
- Inheritance question
- Prev by Date: Re: Inheritance question
- Next by Date: Re: Global application settings for restricted users
- Previous by thread: Re: Inheritance question
- Next by thread: Re: Inheritance question
- Index(es):
Relevant Pages
|