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

Private member

On the book C# for Experienced Programmer by Deitel, page 152, Software
Engineering Observation 5.13:

'When one object of a class has a reference to another object of the same
class, the first object can access all the second object's data and methods
(including those that are private)'

Why?

I am not sure does that refers to:
1) The first object itself is pointing to the second object
OR
2) The first object has a member which is pointing to the second object ?
Nov 16 '05 #1
8 1400
"Alan" <NO**************@yahoo.com.au> wrote in message
news:u6*************@TK2MSFTNGP15.phx.gbl...
'When one object of a class has a reference to another object of the same
class, the first object can access all the second object's data and methods (including those that are private)'


I think I remember coming across this once. Have a look at the code below,
Class1 can call ShowMessage on an instance of itself but class2 cannot.

--
Michael Culley
using System;

namespace TestStuff
{
public class SomeClass
{
public SomeClass()
{
}

private void ShowMessage()
{
System.Windows.Forms.MessageBox.Show("ShowMessage called");
}

public void DoIt(SomeClass SC)
{
SC.ShowMessage();
}
}

public class SomeClass2
{
public SomeClass2()
{
}

public void DoIt(SomeClass SC)
{
SC.ShowMessage();//<---- error
}
}
}
Nov 16 '05 #2
Alan <NO**************@yahoo.com.au> wrote:
On the book C# for Experienced Programmer by Deitel, page 152, Software
Engineering Observation 5.13:

'When one object of a class has a reference to another object of the same
class, the first object can access all the second object's data and methods
(including those that are private)'

Why?

I am not sure does that refers to:
1) The first object itself is pointing to the second object
OR
2) The first object has a member which is pointing to the second object ?


That sounds like a bad explanation for: "Private access is limited by
location of code, not the instance it's operating on."

In other words, any method in a type can use private members of another
instance of the type. For instance:

using System;

public class Test
{
int x;

Test (int x)
{
this.x = x;
}

static void Main(string[] args)
{
Test a = new Test(5);
Test b = new Test(0);
b.Foo(a);
}

void Foo (Test t)
{
Console.WriteLine (t.x);
}
}

Here, Foo called on b is still able to access the private members of t.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #3
This is really bothering me.
Obviously Ive seen this before, but having read further books about OO
concepts, isn't this really breaking the encapsulation of the object? I
would have thought that for strict adherance to OO, access should be limited
by object/instance -or am I misguided?

Br,

Mark.

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Alan <NO**************@yahoo.com.au> wrote:
On the book C# for Experienced Programmer by Deitel, page 152, Software
Engineering Observation 5.13:

'When one object of a class has a reference to another object of the same
class, the first object can access all the second object's data and
methods
(including those that are private)'

Why?

I am not sure does that refers to:
1) The first object itself is pointing to the second object
OR
2) The first object has a member which is pointing to the second object ?


That sounds like a bad explanation for: "Private access is limited by
location of code, not the instance it's operating on."

In other words, any method in a type can use private members of another
instance of the type. For instance:

using System;

public class Test
{
int x;

Test (int x)
{
this.x = x;
}

static void Main(string[] args)
{
Test a = new Test(5);
Test b = new Test(0);
b.Foo(a);
}

void Foo (Test t)
{
Console.WriteLine (t.x);
}
}

Here, Foo called on b is still able to access the private members of t.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 16 '05 #4
Mark Broadbent <no****@nospam.com> wrote:
This is really bothering me.
Obviously Ive seen this before, but having read further books about OO
concepts, isn't this really breaking the encapsulation of the object? I
would have thought that for strict adherance to OO, access should be limited
by object/instance -or am I misguided?


I don't think it really breaks encapsulation. All kinds of things
become basically impossible unless you can do this, IMO. For instance,
implementing Equals in an efficient manner if you don't have access to
private members of both sides.

Admittedly I've only really used Java and C# when it comes to OO
languages (dabbled in C++, but not done a lot) so maybe I'm missing the
problem here - but if I am, it's one that's never bitten me...

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #5
You would also have a problem with creating static factory methods that create instances of otherwise non-creatable classes, e.g.

class Foo
{
private Foo()
{
}
public static Foo CreateFoo()
{
return new Foo();
}
}

(obviously the above example doesn't show a particularly useful class and factory but its the pattern I'm attempting to show - the static member using a private instance constructor)

Regards

Richard Blewett - DevelopMentor
http://staff.develop.com/richardb/weblog

I don't think it really breaks encapsulation. All kinds of things
become basically impossible unless you can do this, IMO. For instance,
implementing Equals in an efficient manner if you don't have access to
private members of both sides.

Nov 16 '05 #6
Its the same rule in both C++ and Delphi.

Cheers

Doug Forster

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP***********************@msnews.microsoft.co m...
Mark Broadbent <no****@nospam.com> wrote:
This is really bothering me.
Obviously Ive seen this before, but having read further books about OO
concepts, isn't this really breaking the encapsulation of the object? I
would have thought that for strict adherance to OO, access should be
limited
by object/instance -or am I misguided?


I don't think it really breaks encapsulation. All kinds of things
become basically impossible unless you can do this, IMO. For instance,
implementing Equals in an efficient manner if you don't have access to
private members of both sides.

Admittedly I've only really used Java and C# when it comes to OO
languages (dabbled in C++, but not done a lot) so maybe I'm missing the
problem here - but if I am, it's one that's never bitten me...

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 16 '05 #7
In Delphi, if classes are put in a same unit(in Delphi term) or file,
private members/methods of a class are accessible by other classes in the
same unit. It seems breaks the OO concepts when I came to learn OO.

"Doug Forster" <doug_ZAPTHIS_AT_ZAPTHIS_TONIQ_DOT_CO_DOT_NZ> wrote in
message news:uH**************@TK2MSFTNGP10.phx.gbl...
Its the same rule in both C++ and Delphi.

Cheers

Doug Forster

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP***********************@msnews.microsoft.co m...
Mark Broadbent <no****@nospam.com> wrote:
This is really bothering me.
Obviously Ive seen this before, but having read further books about OO
concepts, isn't this really breaking the encapsulation of the object? I
would have thought that for strict adherance to OO, access should be
limited
by object/instance -or am I misguided?


I don't think it really breaks encapsulation. All kinds of things
become basically impossible unless you can do this, IMO. For instance,
implementing Equals in an efficient manner if you don't have access to
private members of both sides.

Admittedly I've only really used Java and C# when it comes to OO
languages (dabbled in C++, but not done a lot) so maybe I'm missing the
problem here - but if I am, it's one that's never bitten me...

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


Nov 16 '05 #8
Yes, Delphi has some serious flaws in it's OO concepts. It seems to have
about 3.5 access modifiers ;)

published = public + available for some "reflection style" operations
public = access for all
protected = descendant and unitscope access
private = unitscope access

so to get access to protected members of a class, just declare and use a
"dummy" descendant in the unit where you need access to the protected
members. So effictively "protected" means "public if you want to".
They even included this in Delphi 8 as a feature because the hack is so
widely used, it would break almost all existing code.

Willem van Rumpt

Alan wrote:
In Delphi, if classes are put in a same unit(in Delphi term) or file,
private members/methods of a class are accessible by other classes in the
same unit. It seems breaks the OO concepts when I came to learn OO.

"Doug Forster" <doug_ZAPTHIS_AT_ZAPTHIS_TONIQ_DOT_CO_DOT_NZ> wrote in
message news:uH**************@TK2MSFTNGP10.phx.gbl...
Its the same rule in both C++ and Delphi.

Cheers

Doug Forster

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP***********************@msnews.microsoft. com...
Mark Broadbent <no****@nospam.com> wrote:

This is really bothering me.
Obviously Ive seen this before, but having read further books about OO
concepts, isn't this really breaking the encapsulation of the object? I
would have thought that for strict adherance to OO, access should be
limited
by object/instance -or am I misguided?

I don't think it really breaks encapsulation. All kinds of things
become basically impossible unless you can do this, IMO. For instance,
implementing Equals in an efficient manner if you don't have access to
private members of both sides.

Admittedly I've only really used Java and C# when it comes to OO
languages (dabbled in C++, but not done a lot) so maybe I'm missing the
problem here - but if I am, it's one that's never bitten me...

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too



Nov 16 '05 #9

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

Similar topics

3
by: quo | last post by:
two questions: 1) Does this program demonstrate the basic difference between public and private access? It appears correct to say that instances of a class cannot directly call a private...
5
by: Sandeep | last post by:
Hi, In the following code, I wonder how a private member of the class is being accessed. The code compiles well in Visual Studio 6.0. class Sample { private: int x; public:
4
by: Tom | last post by:
Let's say I've got a class defined in a header file. class foo { private: ... int bar; .... }; Now lets say I have a function that needs to access the private variable
3
by: Richard Webb | last post by:
Hi all, I guess this is more of a design problem than a language problem, but I'm confused either way! I have a class and it has a private data member which is a struct. The size of the struct is...
12
by: Manolis | last post by:
Hi, I was wondering if there is any way to make two objects of the same class to be able to access each other's private data, like this: class A { public: void access( const A& a )...
8
by: __PPS__ | last post by:
Hello everybody, today I had another quiz question "if class X is privately derived from base class Y what is the scope of the public, protected, private members of Y will be in class X" By...
2
by: Christoph Boget | last post by:
Let's take the following class: class MyClass { private int privateVar; public int PublicVar { get { return privateVar; } } public MyClass() {}
6
by: Ajay Martin | last post by:
Why would it be reasonable for someone to argue that it is incorrect to allow a public member inherited from a public base class to be redefined as private?
8
by: David Veeneman | last post by:
Should a member variable be passed to a private method in the same class as a method argument, or should the method simply call the member variable? For years, I have passed member variables to...
14
by: v4vijayakumar | last post by:
Why we need "virtual private member functions"? Why it is not an (compile time) error?
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?
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...
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
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
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...
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...
0
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...

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.