3

Private access allows only objects of the same class to access it. However, is there any way to restrict class members such that only the owner object - and no others regardless of class - has access to it?

As an analogy to real world objects, your butt is private to yourself, and no one else should have access to your butt without your permission just because he/she is a fellow human. Is there a way to implement this in code?

Note: This is a general question, but since I will be using both Java and C++ at work, I would like to see how this can be implemented in each language.

thegreatjedi
  • 2,788
  • 4
  • 28
  • 49
  • In Java, that's not possible. – Luiggi Mendoza Nov 24 '15 at 03:32
  • Although no access specifier to do so but you may still implement the behavior using a bit of logic in your access methods to private fields. – Juned Ahsan Nov 24 '15 at 03:34
  • 1
    Maybe you can make it such that when calling a `getter` for the value you check the current's object's address in memory with the object that is calling it and see if they are equal? Then you know that the `"Owner"` is calling to get the value. – 3kings Nov 24 '15 at 03:34
  • In C++ neither. The keyword `private` is relative to the class, not to the instance. – A.S.H Nov 24 '15 at 03:35
  • But why would you want to do this? What is the use case? – Anon Mail Nov 24 '15 at 03:55
  • @AnonMail it's like the analogy I've given. A person has his own privacy and secrets. Some of those, you would share with your closest of friends, but there's always something that you and only you yourself can know. There's me-and-my-best-buddies private (the current one used in Java & C++ syntax) and then there's the my-darkest-secrets-that-should-never-see-the-light-of-day private. It doesn't seem sufficient by instinct that same-class access is the most restrictive access modifier when reality can go even further. OO models the real world, right? – thegreatjedi Nov 24 '15 at 05:07
  • It's better to have a concrete example. I've put in answer in below. – Anon Mail Nov 24 '15 at 05:26

3 Answers3

2

[http://en.cppreference.com/w/cpp/language/access]

A private member of a class can only be accessed by the members and friends of that class, regardless of whether the members are on the same or different instances.

The same applies to Java. [https://docs.oracle.com/javase/specs/jls/se7/html/jls-6.html#d5e8146]

A private class member or constructor is accessible only within the body of the top level class

There may be workarounds by imposing programming rules to your project, involving the imperative use of setters and getters, but they are difficult to enforce.

The only exception that I personally know about is the smalltalk programming language, where a private member is really private to the object owning it.

A.S.H
  • 29,101
  • 5
  • 23
  • 50
1

This is not possible in neither C++ or Java, both languages use class-level access modifiers instead of object-level ones.

There's a reason for this. See e.g. this Q&A.

Community
  • 1
  • 1
Emil Laine
  • 41,598
  • 9
  • 101
  • 157
0

I think you would have to give a concrete example of what you're trying to do. For me, your analogies are too abstract. C++ doesn't support it directly but depending on what you want to do you may be able to implement it.

One thing I can think of is have protected data in a base class. Let it be a Secrets container (e.g. std::vector<Secrets>). A derived class instance now has access to it's base class' instance data. But no other instance of the derived class has access to it. Of course, the base class has access to it. But you can control what the base class is able to do (e.g you can make it non-copyable, etc).

Anon Mail
  • 4,660
  • 1
  • 18
  • 21