473,659 Members | 3,631 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

'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 16351
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*****@newsgr oup.nospam> wrote in message
news:8A******** *************** ***********@mic rosoft.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*****@newsgr oup.nospam> wrote in message
news:8A******** *************** ***********@mic rosoft.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***@DIESPAMM ERSDIEtakempis. com> wrote in message
news:%2******** *******@TK2MSFT NGP15.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(o bj, 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*****@newsgr oup.nospam> wrote in message
news:8A******** *************** ***********@mic rosoft.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********@yah oo.com> wrote in message
news:eY******** ******@TK2MSFTN GP14.phx.gbl...

"Kevin Spencer" <ke***@DIESPAMM ERSDIEtakempis. com> wrote in message
news:%2******** *******@TK2MSFT NGP15.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(o bj, 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*****@newsgr oup.nospam> wrote in message
news:8A******** *************** ***********@mic rosoft.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
2297
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 in Rectangle, the compiler tells me Point::x is protected. I would have expected Rectangle to see the protected members of any Point. Compiling the following code give me this error: g++ -o rectangle main.cc main.cc: In member function `size_t...
28
3408
by: Act | last post by:
Why is it suggested to not define data members as "protected"? Thanks for help!
7
1961
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
7714
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. They don't actually protect any encapsulation or anything, and for all the actual protection they offer, they might as well be public. For example: class B { protected:
11
3818
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 experts. I would like to produce Javascript classes that can be "subclassed" with certain behaviors defined at subclass time. There are plenty of ways to do this through prototyping and other techniques, but these behaviors need to be static and...
86
4607
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 method can be defined by prefixing the declaration with the keywords: public, protected or private. Public declared items can be accessed everywhere."
6
4130
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 functions of another (same type) - but not via a base class pointer.... I've checked the FAQs, Meyers etc but nothing obvious I can find explains it.
16
3619
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
1932
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 its base class only through a derived object. The derived class has no special access to the protected members of base type objects." He gives the following example: =========================================================
0
8428
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8851
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8748
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8531
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8628
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6181
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4175
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
1978
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1739
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.