473,770 Members | 1,629 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Is [protected and/or internal] to be avoided?

Just getting into OOP here...
I remember reading somewhere that having protected and/or internal member
variables is to generally be avoided or potentially a "bad thing" because
having such members violates the OOP principle of encapsulation. The way it
was presented was something like, "having a protected member implies a high
degree of trust between the base class and any derived classes - because the
derived classes have unrestricted or uncontrolled access to the protected
variable."

I now have a situation where I have a few member variables and methods that
need to be called by derived classes "as is" - so no apparent need [apparent
to me anyway] to make them abstract or virtual as the derived classes have
no need to modify their behavior (speaking of the methods in particular).
This seems to be a perfectly reasonable use of 'protected' scope. Am I
missing something? Or would most of you just shrug this off and say "very
well then."

Thanks.
Sep 18 '07 #1
17 1440
Jeremy S. <A@B.comwrote :
Just getting into OOP here...
I remember reading somewhere that having protected and/or internal member
variables is to generally be avoided or potentially a "bad thing" because
having such members violates the OOP principle of encapsulation. The way it
was presented was something like, "having a protected member implies a high
degree of trust between the base class and any derived classes - because the
derived classes have unrestricted or uncontrolled access to the protected
variable."

I now have a situation where I have a few member variables and methods that
need to be called by derived classes "as is" - so no apparent need [apparent
to me anyway] to make them abstract or virtual as the derived classes have
no need to modify their behavior (speaking of the methods in particular).
This seems to be a perfectly reasonable use of 'protected' scope. Am I
missing something? Or would most of you just shrug this off and say "very
well then."
It's perfectly reasonable to have protected/internal members in general
- but (IMO) not variables. Internal properties, methods and events are
fine - but I always keep variables private.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Sep 18 '07 #2
On Tue, 18 Sep 2007 10:45:31 -0700, "Jeremy S." <A@B.comwrote :
>Just getting into OOP here...
I remember reading somewhere that having protected and/or internal member
variables is to generally be avoided or potentially a "bad thing" because
having such members violates the OOP principle of encapsulation.
Your source was correct.
>The way it
was presented was something like, "having a protected member implies a high
degree of trust between the base class and any derived classes - because the
derived classes have unrestricted or uncontrolled access to the protected
variable."
A good reason.
>
I now have a situation where I have a few member variables and methods that
need to be called by derived classes "as is" - so no apparent need [apparent
to me anyway] to make them abstract or virtual as the derived classes have
no need to modify their behavior (speaking of the methods in particular).
This seems to be a perfectly reasonable use of 'protected' scope. Am I
missing something? Or would most of you just shrug this off and say "very
well then."

Thanks.
There is no problem at all with making methods protected or internal.
For your member variables make them private and set up a
protected/internal property to allow access.

rossum

Sep 18 '07 #3

"Jeremy S." <A@B.comwrote in message
news:e$******** ********@TK2MSF TNGP05.phx.gbl. ..
Just getting into OOP here...
I remember reading somewhere that having protected and/or internal member
variables is to generally be avoided or potentially a "bad thing" because
having such members violates the OOP principle of encapsulation.
Where did u read that?
Nothing farest from the truth.

The way it
was presented was something like, "having a protected member implies a
high degree of trust between the base class and any derived classes -
because the derived classes have unrestricted or uncontrolled access to
the protected variable."
That's correct (the part about unrestricted) but I fail to see something bad
on that.
In case you do not want the variable accesible mark it as private and then
create a protected property

Sep 18 '07 #4
>Just getting into OOP here...
>I remember reading somewhere that having protected and/or internal member
variables is to generally be avoided or potentially a "bad thing" because
having such members violates the OOP principle of encapsulation.

Where did u read that?
Nothing farest from the truth.
I don't recall the source that I clearly recall mentioning the bit about
violating encapsulation.. .. probably on the net somewhere.

But I did take a quick look through some reference books I have handy just
to see if it was perhaps one of them, and I came across something similar:
The book is Framework Design Guidelines -

Page 165: "... Anyone is able to subclass an unsealed class and access
protected members, and so all the same defensive coding practices used for
public memers apply to protected members as well."

Page 166 (Brad Abrams): "... we consider protected and public to be roughly
equivalent. We generally do the same level of review and error checking in
protected APIs as we do in public APIs as they can be called form any code
that just happens to subclass.

---
Sep 18 '07 #5
The key in the book you cite is that the class is unsealed. Someone who
recieves the dll may extend your class in an unforseen way. Unless you
intend an external developer to extend your class, mark it sealed.

"Jeremy S." wrote:
Just getting into OOP here...
I remember reading somewhere that having protected and/or internal member
variables is to generally be avoided or potentially a "bad thing" because
having such members violates the OOP principle of encapsulation.
Where did u read that?
Nothing farest from the truth.

I don't recall the source that I clearly recall mentioning the bit about
violating encapsulation.. .. probably on the net somewhere.

But I did take a quick look through some reference books I have handy just
to see if it was perhaps one of them, and I came across something similar:
The book is Framework Design Guidelines -

Page 165: "... Anyone is able to subclass an unsealed class and access
protected members, and so all the same defensive coding practices used for
public memers apply to protected members as well."

Page 166 (Brad Abrams): "... we consider protected and public to be roughly
equivalent. We generally do the same level of review and error checking in
protected APIs as we do in public APIs as they can be called form any code
that just happens to subclass.

---
Sep 18 '07 #6

"Jeremy S." <A@B.comwrote in message
news:eP******** ********@TK2MSF TNGP04.phx.gbl. ..
>>Just getting into OOP here...
I remember reading somewhere that having protected and/or internal
member variables is to generally be avoided or potentially a "bad thing"
because having such members violates the OOP principle of encapsulation.

Where did u read that?
Nothing farest from the truth.

I don't recall the source that I clearly recall mentioning the bit about
violating encapsulation.. .. probably on the net somewhere.

But I did take a quick look through some reference books I have handy just
to see if it was perhaps one of them, and I came across something similar:
The book is Framework Design Guidelines -

Page 165: "... Anyone is able to subclass an unsealed class and access
protected members, and so all the same defensive coding practices used for
public memers apply to protected members as well."

Page 166 (Brad Abrams): "... we consider protected and public to be
roughly equivalent. We generally do the same level of review and error
checking in protected APIs as we do in public APIs as they can be called
form any code that just happens to subclass.
That's certainly true, but it hardly implies that "protected" is any worse
than "public". In fact, since .NET enforces runtime type checking,
subclassing an unsealed class is NOT sufficient to gain access to protected
members of other runtime types (in C++ it is sufficient, but C++ can always
bypass access checks using pointer arithmetic).
Sep 18 '07 #7
"Jeremy S." <A@B.comwrote in message
news:eP******** ********@TK2MSF TNGP04.phx.gbl. ..
>
Page 165: "... Anyone is able to subclass an unsealed class and access
protected members, and so all the same defensive coding practices used for
public memers apply to protected members as well."

Page 166 (Brad Abrams): "... we consider protected and public to be
roughly equivalent. We generally do the same level of review and error
checking in protected APIs as we do in public APIs as they can be called
form any code that just happens to subclass.
I think is a problem in Java, as that language has no keywords for
virtualness and overrides.
Hence, If extend your class, add a new method, that happens to have the same
name as one of yours,
I have just overriden your method without me even knowing.

C# would not compile, and ask me to override or reintroduce the method,
witch will probably make me rename mine, as I had no intention of overriding
yours.

Also, when you code in asp.net, all thing code-behind for a page
that you want to sport in your aspx/asmx should be protected, not public.

- Michael Starberg
Sep 19 '07 #8
On Sep 19, 12:12 pm, "Michael S" <n...@no.nowrot e:
I think is a problem in Java, as that language has no keywords for
virtualness and overrides.
Well, you can declare a method to be final - but it defaults to being
virtual, yes.
Hence, If extend your class, add a new method, that happens to have the same
name as one of yours,
I have just overriden your method without me even knowing.

C# would not compile, and ask me to override or reintroduce the method,
witch will probably make me rename mine, as I had no intention of overriding
yours.
In C# it would compile, but with a warning.
Also, when you code in asp.net, all thing code-behind for a page
that you want to sport in your aspx/asmx should be protected, not public.
Fortunately that improves in C# 2 with partial classes.

Jon

Sep 19 '07 #9
"Jon Skeet [C# MVP]" <sk***@pobox.co mwrote in message
news:11******** **************@ 50g2000hsm.goog legroups.com...
On Sep 19, 12:12 pm, "Michael S" <n...@no.nowrot e:
>I think is a problem in Java, as that language has no keywords for
virtualness and overrides.

Well, you can declare a method to be final - but it defaults to being
virtual, yes.
Well of course.
Hehe, I wonder if 'virtual sealed' would give me a 'are-you-stupid' error?
<g>
>
>Hence, If extend your class, add a new method, that happens to have the
same
name as one of yours,
I have just overriden your method without me even knowing.

C# would not compile, and ask me to override or reintroduce the method,
witch will probably make me rename mine, as I had no intention of
overriding
yours.

In C# it would compile, but with a warning.
Ah, just tested. It gives a warning alright.
>
>Also, when you code in asp.net, all thing code-behind for a page
that you want to sport in your aspx/asmx should be protected, not public.

Fortunately that improves in C# 2 with partial classes.
This has nothing to do with partial classes.
>
Jon
- Michael Starberg
Sep 19 '07 #10

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

Similar topics

8
2546
by: Carlos J. Quintero | last post by:
Hi, As you know the current keywords "protected internal" (C#) or "Protected Friend" (VB.Net) means "Protected Or internal" (C#) or "Protected Or Friend" (VB.Net), that is, the member is accesible from whichever types inside the assembly or from derived classes included those outside the assembly. The IL has a provision for the "family AND assembly" accesibility level, that is, a member is accesible from derived classes belonging to...
6
12000
by: Sgt. Sausage | last post by:
I know it's not possible, but I need a protected internal interface: protected internal interface ISomeInterface{ // yadda yadda yadda } Basically, I need an interface that is completely accessable from within the assembly (internal), but is not valid outside
2
6062
by: Kolozs, Ãron | last post by:
Hi everybody, The C# compiler reports a Compiler Error CS0052 in the following situation: I declared a type marked as "internal": namespace MyNamespace { internal class MyInteralClass
11
3843
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...
7
6405
by: My4thPersonality | last post by:
1st, simple question, is there a protected in C#? I mean a comparable keyword like the C++ protected, which defines a member as accessable for decendants, but not for the outside world. Then, a more complicated question, is privat in C#, then comparable with protected, or private in C++. Or to say it in other words, if I declare a member private in C#, can I access it in a decendant class?
4
6305
by: newbie120 | last post by:
Hi all maybe its just been a long day, but i have a question about call access modifiers in C#. Consider the following code. namespace Application { private class Class1 { int i;
16
3631
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; }
13
2826
by: Clive Dixon | last post by:
I am refactoring some code to move some base classes into a separate assembly. One of these base classes has a member property which is 'protected internal'. However when I move these base classes to another assembly, the compiler complains that the override in the derived class, also declared as 'protected internal', is trying to change the access modified from 'protected', which is clearly not the case. (I have checked the metadata in...
2
1979
by: jehugaleahsa | last post by:
Hello: I have a public abstract class. The concrete subclasses must pass an instance of an internal class to the abstract class' ctor. I like to make the constructors of my abstract classes protected. However, since it has a parameter that is of an internal type, I am forced to make it internal also. This is fine since no one outside my library can see it.
0
9432
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10059
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...
0
9873
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...
0
8891
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7420
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
6682
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5313
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...
0
5454
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2822
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.