Object-private Vs class-private

Is there an object-private concept in any OOP language? I mean more restrictive than classic private access?

Private (or class-private) restricts access to the class itself. Only those methods that are part of the same class can access private members.

object-private: restricts access to the object itself. Only method objects that can access elements, and it will be impossible to write:

public class Person { private String secret; public String othersSecret; public void snoop(Person p) { othersSecret = p.secret; //will be prohibited by the compiler } 

EDIT:

If it exists, can you give me some examples ... if you do not think it is interesting to have such a function? and is it possible to imitate it in other OOP languages?

EDIT 2: Thank you guys all the answers were very instructive ...

Still a temporary imprisonment :

instance-private exists in two languages:

1 - Smalltalk after hours of browsing the web. I found the language of this concept!

The state that an object has is always private to that object. Other objects can request or change this state only by sending requests (messages) to this object.

2 - Ruby thanks to Logan:

One person summed up by saying that in C ++ “private” means “private to this class”, while in Ruby it means “private to this instance”. This means that in C ++, from class A code, you can access any private method for any other object of type A. In Ruby you cannot: you can only access private methods for your instance of the object, and not for which or any other instance of an object (class A).

+4
source share
7 answers

In ruby, for each private object, only private is used (you need to use protected to get the class’s private behavior).

eg. foo.rb:

  class A private def a=(x) @a=x end public def a @a end def b(c) ca = 2 end end a1 = A.new a2 = A.new a1.b(a2) 

Running it, we get

  foo.rb:12:in `b': private method `a=' called for #<A:0xb7c9b6e0> (NoMethodError) from foo.rb:18 

Of course, there are ways around this, but there are almost always.

+3
source

I think that the function you want can be implemented figuratively, not allowing Personal people to communicate directly. To achieve this with minimal effort, you can enter an interface that does not provide access to the things you want to keep secret.

 public interface IPerson { void communicateFormally(); } public class Person : IPerson { private String secret; public String othersSecret; public void snoop(IPerson p) { othersSecret = p.secret; //will be prohibited by the compiler } ... } 

Now it can be “hacked” by an ugly actor, but I think the problem is one hack.

+2
source

In Java, what it looks like you are writing, "private" means class-private. You cannot force a private mode object. The reason for this is because "private" is a way to force encapsulation, not security.

+1
source

I do not think that this difference between the vs object private class exists for the most common OO languages, such as C #, C ++, Python, Java, Objective-C ... To be fair, I can’t remember a language that itself fact has this function.

0
source

Yes, you can create objects in Java containing instance variables that other instances of this interface do not see. Trivial example:

 class Secretive { } Secretive s = new Secretive() { int unknowable = 42; }; Secretive t = new Secretive() { String unfathomable = "banana"; }; 
0
source
  public class Person { private String privateSecret; public String PublicInformation; public void Snoop(Person p) { // will be allowed by the .NET compiler p.PublicInformation = p.privateSecret; } } 

just use read-only properties or fields to ensure security.

You can also use internal to encapsulate your class in assambley.

You can also use some Deny methods like this one .

0
source

All Articles