473,549 Members | 3,048 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1410
"Alan" <NO************ **@yahoo.com.au > wrote in message
news:u6******** *****@TK2MSFTNG P15.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.MessageBo x.Show("ShowMes sage 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.WriteLi ne (t.x);
}
}

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

--
Jon Skeet - <sk***@pobox.co m>
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.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
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.WriteLi ne (t.x);
}
}

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

--
Jon Skeet - <sk***@pobox.co m>
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.co m>
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.co m> wrote in message
news:MP******** *************** @msnews.microso ft.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.co m>
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_A T_ZAPTHIS_TONIQ _DOT_CO_DOT_NZ> wrote in
message news:uH******** ******@TK2MSFTN GP10.phx.gbl...
Its the same rule in both C++ and Delphi.

Cheers

Doug Forster

"Jon Skeet [C# MVP]" <sk***@pobox.co m> wrote in message
news:MP******** *************** @msnews.microso ft.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.co m>
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_A T_ZAPTHIS_TONIQ _DOT_CO_DOT_NZ> wrote in
message news:uH******** ******@TK2MSFTN GP10.phx.gbl...
Its the same rule in both C++ and Delphi.

Cheers

Doug Forster

"Jon Skeet [C# MVP]" <sk***@pobox.co m> wrote in message
news:MP****** *************** **@msnews.micro soft.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,
implementi ng 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.co m>
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
2308
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 method, but a public method can be called by the instance to invoke the private method. 2) So is it true that only public methods of a class can invoke...
5
2401
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
5706
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
2059
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 what I would call relatively large (about 1Mb). I have written methods for this class so that the struct can be correctly filled with the corerct...
12
2658
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 ) {cout<<"a.value="<<a.value<<endl; } private: int value;
8
7819
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 scope they meant public/protected/private access modifiers :) Obviously all members of the base privately inherited class will be private, and that was...
2
1817
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
1833
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
5728
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 private methods in the same class as method arguments. For example, if Public method Foo() calls private method Bar(), and if Bar() uses member...
14
4171
by: v4vijayakumar | last post by:
Why we need "virtual private member functions"? Why it is not an (compile time) error?
0
7520
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...
0
7446
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...
0
7718
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. ...
0
7956
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...
0
6041
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...
1
5368
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...
0
5088
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...
0
3480
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
763
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...

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.