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

Private Keyword

It seems odd that a private instance member can be accessed by any other
instance of its defining class. For example:
class A
{
private int x = 1;
public void func (A a)
{
Console.Out.WriteLine ( "private member = {0}", a.x);
}
}
Rather than limit 'private' to an instance, C# allows any instance of A to
have access to another instance's private members - as in the above code.
This would be like allowing any student in an 8th grade math class access to
the exam answers written by any other student. It seems like 'private'
should only allow 'this.x' - not 'a.x' access.

If someone can explain this to me I'd be grateful.

Nov 16 '05 #1
6 1726
Well, you'll get a lot of answers on this one...

A short answer is that object based permissions aren't easy to manage
in a static typed system.

Also, often, when one object works with another of its type (as you've
shown above) its for Parameter initialization. This would require some
type of private access...

-Mezz

Nov 16 '05 #2
The accessibility is there more to decouple implementation details of a
class from the code that uses it. This is to enable an implementor of a
class to change the inner workings of it without having to rewrite all
the code that uses the class. In your example all code that access the
private field is still contained within class A, encapsulation is not
broken.

What you are suggesting sounds more like a security framework that is to
be enforced at runtime. It sure is possible that a language could
support this, but I don't think it belongs there. It is more of an
application feature.

Your post did make me wonder though, does anybody know of any
OO-language that support this kind of accessibility modifier? Or
anything like it?

Regards,
Joakim

WillNap wrote:
It seems odd that a private instance member can be accessed by any other
instance of its defining class. For example:
class A
{
private int x = 1;
public void func (A a)
{
Console.Out.WriteLine ( "private member = {0}", a.x);
}
}
Rather than limit 'private' to an instance, C# allows any instance of A to
have access to another instance's private members - as in the above code.
This would be like allowing any student in an 8th grade math class access to
the exam answers written by any other student. It seems like 'private'
should only allow 'this.x' - not 'a.x' access.

If someone can explain this to me I'd be grateful.

Nov 16 '05 #3
Remember the controversy around 'Hijacking .Net' and the private keyword?
http://books.slashdot.org/article.pl...25&mode=nested

ok,
aq

"WillNap" <Wi*****@discussions.microsoft.com> wrote in message
news:5C**********************************@microsof t.com...
It seems odd that a private instance member can be accessed by any other
instance of its defining class. For example:
class A
{
private int x = 1;
public void func (A a)
{
Console.Out.WriteLine ( "private member = {0}", a.x);
}
}
Rather than limit 'private' to an instance, C# allows any instance of A to
have access to another instance's private members - as in the above code.
This would be like allowing any student in an 8th grade math class access to the exam answers written by any other student. It seems like 'private'
should only allow 'this.x' - not 'a.x' access.

If someone can explain this to me I'd be grateful.

Nov 16 '05 #4
All,

Thanks for your remarks. It seems that John Miles said it best:
"Data-hiding is an OOP convention, not a security feature."

WillNap

"Ahmed Qurashi" wrote:
Remember the controversy around 'Hijacking .Net' and the private keyword?
http://books.slashdot.org/article.pl...25&mode=nested

ok,
aq

"WillNap" <Wi*****@discussions.microsoft.com> wrote in message
news:5C**********************************@microsof t.com...
It seems odd that a private instance member can be accessed by any other
instance of its defining class. For example:
class A
{
private int x = 1;
public void func (A a)
{
Console.Out.WriteLine ( "private member = {0}", a.x);
}
}
Rather than limit 'private' to an instance, C# allows any instance of A to
have access to another instance's private members - as in the above code.
This would be like allowing any student in an 8th grade math class access

to
the exam answers written by any other student. It seems like 'private'
should only allow 'this.x' - not 'a.x' access.

If someone can explain this to me I'd be grateful.


Nov 16 '05 #5
Will,

I don't agree with you. A simple sample, there will become probably be a lot
of problems using a public with a lower case starting field with a same name
with an uper case starting public property.

The generated dll's are even not proper for general use in Net

Just my thought,

Cor
Nov 16 '05 #6
Cor,

I agree with your point. John Miles, I think, was speaking to a perspective
of OOP data hinding in general.

Will
"Cor Ligthert" wrote:
Will,

I don't agree with you. A simple sample, there will become probably be a lot
of problems using a public with a lower case starting field with a same name
with an uper case starting public property.

The generated dll's are even not proper for general use in Net

Just my thought,

Cor

Nov 16 '05 #7

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

Similar topics

27
by: gabor | last post by:
hi, as far as i know in python there aren't any private (i mean not accessible from the outside of the object) methods/fields. why? in java/c++ i can make a method private, this way...
20
by: Nemanja Trifunovic | last post by:
Something I don't get it: Say we have: ref class Base { virtual void SomeVirtualFunction() {Console::WriteLine(L"Base");} public: void SomeAccessibleFunction() {SomeVirtualFunction();}
2
by: dumbo | last post by:
Hello, who gives one more shining solution to me? My application load form "A". The form "A" by means of a push-button load form "B" that in its turn by means of a push-button load form "C". Form...
6
by: earthwormgaz | last post by:
Is the following legal? class Outer { class Inner { private: Inner() { } };
63
by: time.swift | last post by:
Coming from a C++ / C# background, the lack of emphasis on private data seems weird to me. I've often found wrapping private data useful to prevent bugs and enforce error checking.. It appears...
7
by: =?Utf-8?B?UHJpdmF0ZQ==?= | last post by:
Hey, Can some one tell me about what's wrong with the following code and what is the solution with some meaningful correction ----------------------------- class Program { static void...
10
by: siddhu | last post by:
Dear Experts, I want to make a class whose objects can be created only on heap. I used the following approach. class ss { ss(){} public: void* operator new(size_t sz)
2
by: arnie20 | last post by:
what is the application of public protected & private keyword? why do we declare some data members of a class as private ?
17
by: Peng Yu | last post by:
Hi, I'm wondering if there is something in namespace like the 'private' keyword in class? I want to define some class or function that can only be used within that namespace. Thanks, Peng
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: 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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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,...
0
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...

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.