views:

265

answers:

7

Is there a notion of object-private in any OOP language ?? I mean more restrictive than the classic private access ?

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

object-private : restricts the access to the object itself. Only methods objects that can access members 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 exist can you give me some examples ... if not do you think it's interesting to have this kind of feature ?? and is it possible to simulate it in others OOP languages ??

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

Until now, the temporary conclusion :

The instance-private notion exists in 2 languages :

1 - Smalltalk after hours of googling :) I found the language behind this concept !!

The state an object holds is always private to that object. Other objects can query or change that state only by sending requests (messages) to the object to do so.

2 - Ruby thanks to Logan :

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

A: 

I don't think this kind of distinction of class vs object private exists for the most common languages OO such as C#, C++, Python, Java, Objective-C ... To be fair I can't remember of a language that actually has this feature.

ruibm
+1  A: 

In Java, which is what it looks like you are writing, "private" means class-private. There is no way to force object-private mode. The reason for this is that "private" is a way of enforcing encapsulation, not security.

Avi
A: 

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

class Secretive { }
Secretive s = new Secretive() {
    int unknowable = 42;
};
Secretive t = new Secretive() {
    String unfathomable = "banana";
};
Jonathan Feinberg
This is not exactly true. In your example, you create two new anonymous classes, and create one instance of each. So each instance can have data that is private to its (anonymous) class, but that's not exactly the same thing as having data private to the instance.
Daniel Pryden
A: 
    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 properties, or readonly fields to enforce your security.

You can use also internal accessor to incapsulate your class in a assambley.

You can also use some Deny techniques like this one.

serhio
it also will be allowed by javac and all oop compiller i know ... I ask if there is a language who offer an other level of encapsulation that prohibit this ...
wj
@Paolo : It's not "bizarre" it's the normal way that OOP langages handle encapsulation ... that's why I asked my question :)
wj
+2  A: 

I think the feature you want could be implemented by, figuratively, not allowing Persons to communicate directly. To achieve this with minimal effort you can introduce an interface, which would not provide access to things you want to make 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, this could be "hacked" by an ugly cast, but I think that's the problem of the one hacking.

Rotsor
but this will give us a "pivate-interface" feature ... not really a "private-object" like "Logan Capaldo" ruby example ...
wj
+3  A: 

In ruby, per-object private is the only private (you have to use protected to get class private behavior).

E.g. foo.rb:

 class A
    private
    def a=(x)
            @a=x
    end
    public
    def a
            @a
    end

    def b(c)
            c.a = 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 almost always are.

Logan Capaldo
Very interesting example !! I'm wondering if there's other languages that support this ... and why it's not the standard in others OOP languages ...
wj
There are all sorts of OO features that only show up in some languages. See CLOS for some interesting ones (e.g. multimethods, metaclasses and method combination (e.g. :before, :after and :around), of which the latter two prefigured aspect-oriented programming).
outis
@outis: what I like most in CLOS is the linearization of calls in multiple inheritance when we use call-next-method ... big up to CLOS ;)
wj
CLOS makes me drool.
outis
+2  A: 

After hours of googling :) I found the language behind this concept : Smalltalk

The state an object holds is always private to that object. Other objects can query or change that state only by sending requests (messages) to the object to do so.

wj
See pdf "Encapsulation and Inheritance in Object-Orlented Programming Languages" http://eprints.kfupm.edu.sa/37524/1/37524.pdf
igouy
See "Design Principles Behind Smalltalk" http://www.cs.virginia.edu/~evans/cs655/readings/smalltalk.html
igouy
@igouy: thx for the articles ...
wj