Object-Oriented modifiers : public/private ... (please tell me which one i'm looking for)

I work with C#, but i think this question applies to other OO languages as well:

With a member (or variable) that is declared with PRIVATE, i can access it from any objects of this class -- which i think is not very appropriate since it is not "private" enough. The following demonstrates my idea (which leads to my question):

=======================================
class ClassA {
  private x;
  public void ModX(ClassA y) {
    y.x = something;
  }
}

ClassA a1 = new ClassA();
ClassA a2 = new ClassA();
a2.ModX(a1);   // this line is modifying a1.x via a2.
======================================

The above code is valid in C# and I believe it should be valid in Java as well. However, the last line is modifying a1.x via a2, which i don't like.

Is there a way to make a private variable such that it can only be accessed by its holding object? E.g. in the above example, only object a1 can have access to a1.x, and a2 cannot have access to a1.x although they belong to the same class.


jtjliAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

arnoldCommented:
Is this a school excersise or is this an April Fools joke?

try a1.x=something;

What is the result?

0
masnrockCommented:
It's valid in Java.

Unfortunately, those sort of accesses are only class level, not instance level. So I can understand why you'd be frustrated in trying to do that.

The best thing you can really do is have your program defining out exactly how someone is supposed to work with it. Don't write any methods to do exactly what you're describing You can stay with the classic modifier/accessor methodology, where you have functions/methods do each. But the modifiers take in values, not instances of the class.

Not really what you'd like to hear, but I hope that answers your question.
0
InteractiveMindCommented:
> private x;
??
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

nayernaguibCommented:
This is not a drawback of Object Oriented Programming at all!

Don't forget that it is *you* who write the implementation of the class you design, and therefore, it is you who have control over how class members should "use" objects instantiated from your class. Additionally, you have the "protected" access modifier, which allows you to choose which members to make "visible" to classes inheriting your class, and which members to keep completely out of reach for any other class.

On the other hand, imagine what could happen if you can't access private members of another object of the same class! You won't even be able to overload operators!!

Take a look at this simple example:

___________________________________________

      class Point
      {
            private int X;
            private int Y;

            public Point(int a, int b)
            {
                  X=a;
                  Y=b;
            }

            public static Point operator +(Point a, Point b)
            {
                  return new Point(a.X+b.X, a.Y+b.Y);
            }
      }

___________________________________________

If the Object Oriented Model worked the way you want, you would have had to make the X and Y members of the Point class *public* in order to be able to overload the "+" operator! If this was the case, you would allow programmers to directly access the X and Y values of your class. If, for example, you write such a class to represent point locations on the screen, then having these members declared public, you could probably see things like X=-245, Y=35464646.

_______________

  Nayer Naguib
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
nayernaguibCommented:
Also, remember that the purpose of having some members declared private is not security. A lot of languages provide the source code of their class libraries. And for others which do not provide their source code, we have the wonderful disassemblers! :-)

Therefore, the only purpose of using access modifiers is to prevent "decent" programmers from messing "unintentionally" with your critical data, something which may lead to unexpected results. And of course, when it comes to understanding the use of each of *your* class members, *you* are the best! :-)

For example, you are not supposed to overload the "+" and "-" operators for your class, without adding checks to make sure that the resultant object will have member values within accepted ranges.

_______________

  Nayer Naguib
0
brettmjohnsonCommented:
If you want to limit the modification to the specific instance, make sure that the passed instance is the invoked instance.  You could do something like:

public void ModX(ClassA y) {
      if (y == this)
            x = something;
      else
            throw new HissyFitException();
}

But if you want to limit the modification to only the invoked instance, change the method parameters, dropping the supplied instance.  Note that I changed the name of the function as well to fit the traditional getter/setter pattern.  In this way, you set the x member for only the invoked instance.

public void SetX(int something) {
      x = something;
}
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.