Re: Inheritance question

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



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)?
>
>


.



Relevant Pages

  • Re: Writing bulletproof code
    ... This support already exists. ... > In fact inheritance is most frequently used to extend a type with ... > A subclass inherits the data and virtual methods of its base class. ... > subclass a superset of the base class, not a subset of the base class. ...
    (comp.programming)
  • Re: Very Confused on Page 33
    ... On page 33 Bruce is explaining inheritance. ... > base class you can also send to objects of the derived class. ... The programmatic difference enables the subclass ...
    (comp.lang.java.help)
  • Re: Writing bulletproof code
    ... In fact inheritance is most frequently used to extend a type with ... A subclass inherits the data and virtual methods of its base class. ... It then adds data, adds methods, or simply overrides methods of the ...
    (comp.programming)
  • Re: Question about inheritance of private variables
    ... >> Most books say private members are not inherited. ... >> subclass using public accessor methods define in the superclass. ... > application of subtle semantics nuances to the terms inheritance, ... > private variable defined by the base class. ...
    (comp.lang.java.programmer)
  • Re: Did I write a good (efficient) program?
    ... The priniples of object-oriented design do not include ... the use of inheritance to overcome inadequate or faulty ... modifying a base class may be ... principles to those of the paradigm. ...
    (comp.lang.cobol)