473,378 Members | 1,527 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,378 software developers and data experts.

'protected' vs. public members

AFAIK there are two ways to expose members of a base class to a derived or
child class:
1. declare the members public in the base class
2. declare them as 'protected' in the base class

Is either of these methods generally recommended over the other? If yes,
why?

Thanks!
Mar 2 '06 #1
8 16336
Protected is like Private, except that Protected members can be accessed by
the base class, and child classes. Private members are not available to
child classes.

If you want your members to be accessed by child classes AND other classes,
use public. If you don't want other classes to access your member, but only
your child classes, use protected.

"Jordan" wrote:
AFAIK there are two ways to expose members of a base class to a derived or
child class:
1. declare the members public in the base class
2. declare them as 'protected' in the base class

Is either of these methods generally recommended over the other? If yes,
why?

Thanks!

Mar 2 '06 #2
Recomendation is design specific. 'Protected' declares that access is
limited to the containing class/types derived from its class.In other
words, it makes variable secured within original and derived classes.

--
Michael Nemtsev :: blog: http://spaces.msn.com/laflour
"At times one remains faithful to a cause only because its opponents do

not
cease to be insipid." (c) Friedrich Nietzsche

Mar 2 '06 #3
General rule of thumb: Use the least amount of access you anticipate
needing, to make the best use of Encapsulation.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
A brute awe as you,
a Metallic hag entity, eat us.
"rmacias" <rm*****@newsgroup.nospam> wrote in message
news:8A**********************************@microsof t.com...
Protected is like Private, except that Protected members can be accessed
by
the base class, and child classes. Private members are not available to
child classes.

If you want your members to be accessed by child classes AND other
classes,
use public. If you don't want other classes to access your member, but
only
your child classes, use protected.

"Jordan" wrote:
AFAIK there are two ways to expose members of a base class to a derived
or
child class:
1. declare the members public in the base class
2. declare them as 'protected' in the base class

Is either of these methods generally recommended over the other? If yes,
why?

Thanks!

Mar 2 '06 #4
So then is it accurate to say that if a base class is virtual then there is
really no point in having public members (therefore make them protected)?


"rmacias" <rm*****@newsgroup.nospam> wrote in message
news:8A**********************************@microsof t.com...
Protected is like Private, except that Protected members can be accessed
by
the base class, and child classes. Private members are not available to
child classes.

If you want your members to be accessed by child classes AND other
classes,
use public. If you don't want other classes to access your member, but
only
your child classes, use protected.

"Jordan" wrote:
AFAIK there are two ways to expose members of a base class to a derived
or
child class:
1. declare the members public in the base class
2. declare them as 'protected' in the base class

Is either of these methods generally recommended over the other? If yes,
why?

Thanks!

Mar 2 '06 #5
Jordan wrote:
So then is it accurate to say that if a base class is virtual then there is
really no point in having public members (therefore make them protected)?


Classes can't be virtual - do you mean abstract?

As for there being no point in having public members - no, if you want
to be able to get at a member from outside the inheritance hierarchy,
you *have* to make the member public (or internal).

Jon

Mar 2 '06 #6

"Kevin Spencer" <ke***@DIESPAMMERSDIEtakempis.com> wrote in message
news:%2***************@TK2MSFTNGP15.phx.gbl...
General rule of thumb: Use the least amount of access you anticipate
needing, to make the best use of Encapsulation.
That's the general OO wisdom - but that also means that either (a) the class
has to be extremely well designed or (b) you need to be clairvoyant to the
extent that you know beyond a shadow of a doubt what memebers may be needed
by people using your classes. (This is usually only a problem with extremely
complex classes.) If the consumer has the source, it's okay becuase they can
make changes.

I can't tell you how many times I can see the information I want in the
debugger as a private memember, but the library/framework writer has deemed
in their infinite wisdom that I don't need to see it. (final/sealed are also
very annoying.) It would be nice if languages had more control - maybe
levels like public(1), public(2) (or using attributes), for beginng vs.
advanced access (and maybe advanced access needs a digital signature from
the publisher), or something like member_access(obj, null/*perm*/,
_privMember) for when you know you're being a bad boy or girl but need to do
it anyway - this would also allow consumer-side source patches in sublcasses
until a new release could be delivered by the vendor. (I suppose reflection
is possible if the lib isn't obfuscated, etc.) Not that the CLR needs to
provide a fine-grained capibility architecture, but some additional access
control is probably useful.

Not that your rule isn't correct in general...

m

--
HTH,

Kevin Spencer
Microsoft MVP
.Net Developer
A brute awe as you,
a Metallic hag entity, eat us.
"rmacias" <rm*****@newsgroup.nospam> wrote in message
news:8A**********************************@microsof t.com...
Protected is like Private, except that Protected members can be accessed
by
the base class, and child classes. Private members are not available to
child classes.

If you want your members to be accessed by child classes AND other
classes,
use public. If you don't want other classes to access your member, but
only
your child classes, use protected.

"Jordan" wrote:
AFAIK there are two ways to expose members of a base class to a derived
or
child class:
1. declare the members public in the base class
2. declare them as 'protected' in the base class

Is either of these methods generally recommended over the other? If yes,
why?

Thanks!


Mar 2 '06 #7
Hi Mike,

I too have been frustrated with this sort of encapsulation. But I can
understand it. If you market a class, and provide people with the ability to
abuse it, you can have a support nightmare on your hands. In addition, it
can work in your favor as a software development business.

As for being extremely well-designed, that is of course another rule of
thumb! ;-)

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
A brute awe as you,
a Metallic hag entity, eat us.
"Mike" <vi********@yahoo.com> wrote in message
news:eY**************@TK2MSFTNGP14.phx.gbl...

"Kevin Spencer" <ke***@DIESPAMMERSDIEtakempis.com> wrote in message
news:%2***************@TK2MSFTNGP15.phx.gbl...
General rule of thumb: Use the least amount of access you anticipate
needing, to make the best use of Encapsulation.


That's the general OO wisdom - but that also means that either (a) the
class has to be extremely well designed or (b) you need to be clairvoyant
to the extent that you know beyond a shadow of a doubt what memebers may
be needed by people using your classes. (This is usually only a problem
with extremely complex classes.) If the consumer has the source, it's okay
becuase they can make changes.

I can't tell you how many times I can see the information I want in the
debugger as a private memember, but the library/framework writer has
deemed in their infinite wisdom that I don't need to see it. (final/sealed
are also very annoying.) It would be nice if languages had more control -
maybe levels like public(1), public(2) (or using attributes), for beginng
vs. advanced access (and maybe advanced access needs a digital signature
from the publisher), or something like member_access(obj, null/*perm*/,
_privMember) for when you know you're being a bad boy or girl but need to
do it anyway - this would also allow consumer-side source patches in
sublcasses until a new release could be delivered by the vendor. (I
suppose reflection is possible if the lib isn't obfuscated, etc.) Not
that the CLR needs to provide a fine-grained capibility architecture, but
some additional access control is probably useful.

Not that your rule isn't correct in general...

m

--
HTH,

Kevin Spencer
Microsoft MVP
.Net Developer
A brute awe as you,
a Metallic hag entity, eat us.
"rmacias" <rm*****@newsgroup.nospam> wrote in message
news:8A**********************************@microsof t.com...
Protected is like Private, except that Protected members can be accessed
by
the base class, and child classes. Private members are not available to
child classes.

If you want your members to be accessed by child classes AND other
classes,
use public. If you don't want other classes to access your member, but
only
your child classes, use protected.

"Jordan" wrote:

AFAIK there are two ways to expose members of a base class to a derived
or
child class:
1. declare the members public in the base class
2. declare them as 'protected' in the base class

Is either of these methods generally recommended over the other? If
yes,
why?

Thanks!



Mar 2 '06 #8
I think that you mean "abstract," not "virtual."

Whether something should be public or protected has to do with what you
want your class to "look like" on the outside. There is no broad rule
of thumb you can blindly apply to get things right the first time. All
you can do is think about the many considerations and then decide on an
overall design for your class hierarchy. Some things to consider.

1. For simple class design, you must remember that there are two
potential "outside users" of your class: other, unrelated classes and
child classes (that inherit from your class). When designing the
functionality of your class, keep it clear what functions are part of
the class's public functionality, and what functions are appropriate
only for inheriting classes. Here I use the word "function" to indicate
method or property.

For examples, look at the Framework classes. Many of them have On...
methods that invoked whenever an event happens. It's not appropriate
that "complete strangers" be able to call these methods and have the
class act as though it's raising an event. They're there to make coding
child classes easier, so they're "protected".

2. Remember that you have to support whatever you export, and if you
export protected methods and properties than you have to support two
outside users: the public (unrelated) user and the protected (child
class) developer. However, if you have an abstract class then of course
you need to support inheritors... that's what the class is for.

3. For more sophisticated design there are also "internal" and
"protected internal" access modifiers that allow you to limit the use
of methods and properties to classes inside your own assembly.

Anyway, the point is that when you design a class from the outset, you
have to sit down and decide what "public" functionality it's going to
provide. If it's an abstract class, or you intend that others inherit
from it (abstract or not) then you have to do that exercise twice: once
for "public" functionality and once for "protected" functionality for
eventual inheritors.

The decision as to what protection each class member receives has to do
with the intended purpose of the member, and no arbitrary rule can tell
you that.

Mar 2 '06 #9

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

Similar topics

2
by: Steven T. Hatton | last post by:
I find the surprising. If I derive Rectangle from Point, I can access the members of Point inherited by Rectangle _IF_ they are actually members of a Rectangle. If I have a member of type Point...
28
by: Act | last post by:
Why is it suggested to not define data members as "protected"? Thanks for help!
7
by: John A. Byerly | last post by:
Hi, I have a couple of classes that are causing compile errors. I hope someone can shed some light as to why. class A { public: A(X* pX) : m_pX(pX) {};
13
by: Adam H. Peterson | last post by:
I just made an observation and I wondered if it's generally known (or if I'm missing something). My observation is that static protected members are essentially useless, only a hint to the user. ...
11
by: Kevin Prichard | last post by:
Hi all, I've recently been following the object-oriented techiques discussed here and have been testing them for use in a web application. There is problem that I'd like to discuss with you...
86
by: jopperdepopper | last post by:
Hi, finally giving php 5 a go, and going over the new approach to classes. Can someone clarify the public, private and protected to me? I quote the php manual: "The visibility of a property or...
6
by: Rick | last post by:
Hi, Can anyone explain to me why the below fails to compile - seeing otherA->f(); as a call to a inaccessible function, while otherB->f(); is ok? It seems you can happily access protected...
16
by: Fir5tSight | last post by:
Hi All, I have a small C#.NET program that is as follows: using System; class A { protected int x = 123; }
2
by: t | last post by:
Lippman's C++ Primer, 4th ed., p562, dicussion of protected members seems to be wrong, unless I am misinterpreting things. He says: "A derived class object may access the protected members of...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.