473,386 Members | 1,743 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

hiding inherited public member


Just curious if there is a way to hide a public member inherited from
another class. I know you can 'new' to give it your own version, but
is there a way to make it no longer public period? I would like the
base class version to stay there, just be protected instead. I can't
make the baseclass version protected because there are times when the
baseclass can be used by itself.
Nov 16 '05 #1
8 4001
Allen... You may want to reconsider the design. With few exceptions,
protected/public data is discouraged.

// start quote

RESPONSE: bs@research.att.com (Bjarne Stroustrup), 13 Jun 95

I think that overstates the case against `protected' a bit. here is
the actual quote:

Five years or so later, Mark banned the use of protected data
members in Interviews because they had become a source of bugs:
``novice users poking where they shouldn't in ways that they ought
to have known better than.'' They also seriously complicate
maintenance: ``now it would be nice to change this, do you think
someone out there might have used it?'' Barbara Liskov's OOPSLA
keynote gives a detailed explanation of the theoretical and
practical problems with access control based on the `protected'
notion. In my experience, there have always been alternatives
to placing significant amounts of information in a common base
class for derived classes to use directly. In fact, one of my
concerns about `protected' is exactly that it makes it too easy
to use a common base the way one might sloppily have used global
data.

Fortunately, you don't have to use protected data in C++;
`private' is the default in classes and is usually the better
choice. Note that none of these objections are significant for
protected member functions. I still consider `protected'
a fine way of specifying operations for use in derived classes.
The Design and Evolution of C++, sec13.9.

However, I do consider reliance on protected data a dubious practice
in any language.

// end quote

Regards,
Jeff
I can't make the baseclass version protected because there are times

when
the baseclass can be used by itself.<
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #2
No matter what you do, you can't prevent someone from doing:

Derived d = new Derived();

Base b = (Base)d;

b.Foo = "not hidden anymore";

-Jason

Allen Anderson wrote:
Just curious if there is a way to hide a public member inherited from
another class. I know you can 'new' to give it your own version, but
is there a way to make it no longer public period? I would like the
base class version to stay there, just be protected instead. I can't
make the baseclass version protected because there are times when the
baseclass can be used by itself.

Nov 16 '05 #3
Your missing my point. I realize the OOP implications of this and
honestly it doesn't really apply to this situation. The problem I
have is that I have created a nice listview which has an Items class
that is public. I have created a new control (TreeListView) that
inherits from my listview. This is all going very well. The only
catch is that now I have 'Nodes' instead of 'Items' and they are
functionally different. I would therefore like to hide the Items
collection. I'm not sure you can do this with c# (im pretty sure you
can't at this point). It's not something that is going to seriously
hurt or damage my control in any way. It would just be a 'nice to
have'. I want to make it protected instead of private in order to
make that Items collection available to anyone subclassing the
treelist that really wants to get at the underlying framework but
again this isn't really crucial.

On Sun, 16 May 2004 14:09:40 -0700, Jeff Louie <je********@yahoo.com>
wrote:
Allen... You may want to reconsider the design. With few exceptions,
protected/public data is discouraged.

// start quote

RESPONSE: bs@research.att.com (Bjarne Stroustrup), 13 Jun 95

I think that overstates the case against `protected' a bit. here is
the actual quote:

Five years or so later, Mark banned the use of protected data
members in Interviews because they had become a source of bugs:
``novice users poking where they shouldn't in ways that they ought
to have known better than.'' They also seriously complicate
maintenance: ``now it would be nice to change this, do you think
someone out there might have used it?'' Barbara Liskov's OOPSLA
keynote gives a detailed explanation of the theoretical and
practical problems with access control based on the `protected'
notion. In my experience, there have always been alternatives
to placing significant amounts of information in a common base
class for derived classes to use directly. In fact, one of my
concerns about `protected' is exactly that it makes it too easy
to use a common base the way one might sloppily have used global
data.

Fortunately, you don't have to use protected data in C++;
`private' is the default in classes and is usually the better
choice. Note that none of these objections are significant for
protected member functions. I still consider `protected'
a fine way of specifying operations for use in derived classes.
The Design and Evolution of C++, sec13.9.

However, I do consider reliance on protected data a dubious practice
in any language.

// end quote

Regards,
Jeff
I can't make the baseclass version protected because there are times

when
the baseclass can be used by itself.<
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!


Nov 16 '05 #4
Allen... Please note that my comment is directed toward a
protected/public
data member, not a public property. When I _had_ to deny access to a
public
property, I have resorted to containment.

Regards,
Jeff

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #5
Jeff Louie <je********@yahoo.com> wrote:
Allen... Please note that my comment is directed toward a
protected/public data member, not a public property. When I _had_ to
deny access to a public property, I have resorted to containment.


I think you mean "field" rather than "member" then. Properties, methods
and fields are *all* members. Discouraging public members would make it
pretty hard for anything to call anything else!

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #6
The only way to do this is to throw an NotImplementedException or
InvalidOperationException or similar by a call to the Items property.

--
cody

Freeware Tools, Games and Humour
http://www.deutronium.de.vu || http://www.deutronium.tk
"Allen Anderson" <al***@sparkysystems.com> schrieb im Newsbeitrag
news:la********************************@4ax.com...
Your missing my point. I realize the OOP implications of this and
honestly it doesn't really apply to this situation. The problem I
have is that I have created a nice listview which has an Items class
that is public. I have created a new control (TreeListView) that
inherits from my listview. This is all going very well. The only
catch is that now I have 'Nodes' instead of 'Items' and they are
functionally different. I would therefore like to hide the Items
collection. I'm not sure you can do this with c# (im pretty sure you
can't at this point). It's not something that is going to seriously
hurt or damage my control in any way. It would just be a 'nice to
have'. I want to make it protected instead of private in order to
make that Items collection available to anyone subclassing the
treelist that really wants to get at the underlying framework but
again this isn't really crucial.

On Sun, 16 May 2004 14:09:40 -0700, Jeff Louie <je********@yahoo.com>
wrote:
Allen... You may want to reconsider the design. With few exceptions,
protected/public data is discouraged.

// start quote

RESPONSE: bs@research.att.com (Bjarne Stroustrup), 13 Jun 95

I think that overstates the case against `protected' a bit. here is
the actual quote:

Five years or so later, Mark banned the use of protected data
members in Interviews because they had become a source of bugs:
``novice users poking where they shouldn't in ways that they ought
to have known better than.'' They also seriously complicate
maintenance: ``now it would be nice to change this, do you think
someone out there might have used it?'' Barbara Liskov's OOPSLA
keynote gives a detailed explanation of the theoretical and
practical problems with access control based on the `protected'
notion. In my experience, there have always been alternatives
to placing significant amounts of information in a common base
class for derived classes to use directly. In fact, one of my
concerns about `protected' is exactly that it makes it too easy
to use a common base the way one might sloppily have used global
data.

Fortunately, you don't have to use protected data in C++;
`private' is the default in classes and is usually the better
choice. Note that none of these objections are significant for
protected member functions. I still consider `protected'
a fine way of specifying operations for use in derived classes.
The Design and Evolution of C++, sec13.9.

However, I do consider reliance on protected data a dubious practice
in any language.

// end quote

Regards,
Jeff
I can't make the baseclass version protected because there are times

when
the baseclass can be used by itself.<
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 16 '05 #7
Another way would be to derive your control from UserControl and put a
ListView/TreeView/Whatever in it.

--
cody

Freeware Tools, Games and Humour
http://www.deutronium.de.vu || http://www.deutronium.tk
"Allen Anderson" <al***@sparkysystems.com> schrieb im Newsbeitrag
news:la********************************@4ax.com...
Your missing my point. I realize the OOP implications of this and
honestly it doesn't really apply to this situation. The problem I
have is that I have created a nice listview which has an Items class
that is public. I have created a new control (TreeListView) that
inherits from my listview. This is all going very well. The only
catch is that now I have 'Nodes' instead of 'Items' and they are
functionally different. I would therefore like to hide the Items
collection. I'm not sure you can do this with c# (im pretty sure you
can't at this point). It's not something that is going to seriously
hurt or damage my control in any way. It would just be a 'nice to
have'. I want to make it protected instead of private in order to
make that Items collection available to anyone subclassing the
treelist that really wants to get at the underlying framework but
again this isn't really crucial.

On Sun, 16 May 2004 14:09:40 -0700, Jeff Louie <je********@yahoo.com>
wrote:
Allen... You may want to reconsider the design. With few exceptions,
protected/public data is discouraged.

// start quote

RESPONSE: bs@research.att.com (Bjarne Stroustrup), 13 Jun 95

I think that overstates the case against `protected' a bit. here is
the actual quote:

Five years or so later, Mark banned the use of protected data
members in Interviews because they had become a source of bugs:
``novice users poking where they shouldn't in ways that they ought
to have known better than.'' They also seriously complicate
maintenance: ``now it would be nice to change this, do you think
someone out there might have used it?'' Barbara Liskov's OOPSLA
keynote gives a detailed explanation of the theoretical and
practical problems with access control based on the `protected'
notion. In my experience, there have always been alternatives
to placing significant amounts of information in a common base
class for derived classes to use directly. In fact, one of my
concerns about `protected' is exactly that it makes it too easy
to use a common base the way one might sloppily have used global
data.

Fortunately, you don't have to use protected data in C++;
`private' is the default in classes and is usually the better
choice. Note that none of these objections are significant for
protected member functions. I still consider `protected'
a fine way of specifying operations for use in derived classes.
The Design and Evolution of C++, sec13.9.

However, I do consider reliance on protected data a dubious practice
in any language.

// end quote

Regards,
Jeff
I can't make the baseclass version protected because there are times

when
the baseclass can be used by itself.<
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 16 '05 #8
yea, I've decided to go in a different direction with my solution to
this. I'm going to write a treeview control (pretty easy actually) to
pair with my listview. Then I'm going to do a treelist by containment
of both.

On Sun, 16 May 2004 21:41:49 -0700, Jeff Louie <je********@yahoo.com>
wrote:
Allen... Please note that my comment is directed toward a
protected/public
data member, not a public property. When I _had_ to deny access to a
public
property, I have resorted to containment.

Regards,
Jeff

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!


Nov 16 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: thechaosengine | last post by:
Hi all, Is there a way to hide a member in a subclass that has been inherited from a base class? Lets leave aside any issues regarding whether its a good idea for a moment. Here's an example...
11
by: Lorenzo Villari | last post by:
I premise I don't know C++ well but... I wondered what is this data hiding thing... I mean, if I can look at the header (and i need it beacuse of the class), then what's hidden? Can someone give...
1
by: Dave | last post by:
Hello NG, Regarding access-declarations and member using-declarations as used to change the access level of an inherited base member... Two things need to be considered when determining an...
6
by: Microsoft | last post by:
Base class: class AssetBase { string _clli; public string CLLI { get
4
by: Fabio Cannizzo | last post by:
Is there a way to hide an inherited protected member so that it is no longer accessible nor visible from the inherited classes? The code below makes c1.foo() no longer accessible, but c2.foo is...
7
by: Dennis | last post by:
I have a class named myclass that inheirits from "baseclass". There is a property of "baseclass" that I don't want exposed in the IDE. The MSDN documentation says" "A derived type can hide an...
0
by: Keenath | last post by:
Is it possible for an inheritor class to hide one of its parents' public functions? I don't mean just replacing the functionality, but to make it such that "Child.X()" is not a valid call, even...
7
by: Mike | last post by:
According to the MSDN (see reference below), it is possible to change a base class function from public to private. How is this done? I've tried using "new", but the function is still accessible. ...
162
by: Sh4wn | last post by:
Hi, first, python is one of my fav languages, and i'll definitely keep developing with it. But, there's 1 one thing what I -really- miss: data hiding. I know member vars are private when you...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.