8
Today while I was taking a course in Udacity (Intro to Java Programming) I thought about this in the exercise Update the class Person
(lesson 3 29/45).
In object-oriented modeling we have the concept of encapsulation (hide implementation) that has no direct relation to protection, but observing one of the purposes of the POO which is to provide a closer modeling of the organization in the real world, why John can read the "thoughts" of Mary and not ask her?
Class example
class Pessoa {
// Pensamentos sao privados
private List<String> pensamentos;
public void lePensamentos(Pessoa outraPessoa) {
// Por que uma pessoa pode ler os pensamentos de outra pessoa?
System.out.println(outraPessoa.pensamentos);
}
}
Example of "improper" access to the thoughts of another object
Pessoa joao = new Pessoa();
Pessoa maria = new Pessoa();
//João é um objeto em um mundo virtual que representa uma pessoa e seu comportamento deve ser como esperado no mundo real!
//João está curioso para saber o que Maria está pensando então ele chama um método para acabar com sua curiosidade
maria.fazPergunta("O que você está pensando Maria?");
//João percebe que ela não quis responder e achou uma solução! Acessar diretamente seus pensamentos (superpoderes!)
joao.lePensamentos(maria); // isso funciona!
The Java language offers some protection feature in this regard? In Java do we have the feature of isolating parts of memory for security reasons? (This question is only for reflection or extra comment)
Clarifications
- The example shows situations where
joão
(instance) accesses attributes/fields of Mary that should be particular only to her. - The word "private" is more in another context than access modifiers in Java, here I put this word as if it were "something particular" that only the instance should have direct access to (think of other attributes also as an attribute that holds someone’s "password", etc.).
So I asked the question if in Java we have the feature of isolating parts of memory for security reasons.
I don’t understand the connection between your reasoning and the informed code. If thoughts are public, it will be accessible even if it is private, this call will give error.
– user28595
@diegofm the code was just a joke, think of the question: Why instances of one class can access private fields from each other?
– gzinho
They cannot, private field is accessible only by that instance. There is no sharing of private methods, nor if there is inheritance between classes.
– user28595
http://stackoverflow.com/questions/30604431/why-can-an-instance-of-a-class-access-private-fields-of-another-instance-of-its
– gzinho
@diegofm, you cannot? =)
– gzinho
I still haven’t connected the rationale of the question to the code. There the code shows a class accessing its own private member, its code is doing something completely different. If thoughts are private, the way you’re doing in this code, you won’t even compile.
– user28595
read more calmly the @diegofm example, see "foo.secret = 100;" it used "foo" which is another instance of Foo.
– gzinho
Yes, not here. But hope that someone who might understand can answer you. I really didn’t understand code with doubt.
– user28595
I think the confusion is that you only spoke of John, but at no time do you use him, except in the comments.
– Randrade
I think I’ve seen this problem of John and Mary mixing thoughts on the site, but I can’t remember where it was. I’ll do a search, and if you find the link to the post I paste here.
– Bacco
@Randrade, I thought the comments were enough. The mistake was mine, I should have made it clear that John was the one who was accessing Mary. I thought because I only had 2 objects there would be no need, but that’s okay! Living and learning! Thanks for the comments!
– gzinho
No problem. I only spoke because the clearer your question, the greater the chance to get good quality answers.
– Randrade
@guiwp I took the liberty of making your title a little clearer. As it stood, I was inducing the error of understanding that occurred here.
– Pablo Almeida
@Pabloalmeida, thank you very much
– gzinho
guiwp, I think you could change your example to become more didactic. If your example included a method
void leMente(Pessoa outraPessoa) { System.out.println(outraPessoa.pensamentos); }
and a calljoao.leMente(maria);
there would be no room left for confusion.– Anthony Accioly
@Anthonyaccioly vc gave a good suggestion, feel free to modify! =)
– gzinho
guiwp, done. Take a look to see if you agree with the editions.
– Anthony Accioly