Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


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

Posted on 2006-04-01
Medium Priority
Last Modified: 2013-11-23
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.

Question by:jtjli
LVL 81

Expert Comment

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

try a1.x=something;

What is the result?

LVL 33

Expert Comment

ID: 16349879
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.
LVL 25

Expert Comment

ID: 16350074
> private x;
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 14

Accepted Solution

nayernaguib earned 2000 total points
ID: 16350117
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)

            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
LVL 14

Expert Comment

ID: 16350223
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
LVL 23

Expert Comment

ID: 16351854
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;
            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;

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
If you are a mobile app developer and especially develop hybrid mobile apps then these 4 mistakes you must avoid for hybrid app development to be the more genuine app developer.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses

577 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question