SCJP confusing question

Hello guys,

Recently I came across one web-site with mock-up SCJP questions. One of the questions and especially provided answer to it really grabs my attention:

Can one object access a private variable of another object of the same class?

Answer: Yes.

Private means "private to the class", NOT "private to the object". So two objects of the same class could access each other's private data.

If it would be true it violates the encapsulation principle of OO. Can you please explain this question/answer because it is really confusing me.

[583 byte] By [Commona] at [2007-11-26 15:28:04]
# 1

public class TouchPrivates {

public static void main(String[] argv) {

ItsPrivate a = new ItsPrivate(1);

ItsPrivate b = new ItsPrivate(2);

System.out.println(b);

a.change(b, 3);

System.out.println(b);

}

private static class ItsPrivate {

private int i;

ItsPrivate(int i) {

this.i = i;

}

public String toString() {

return "It's " + i;

}

public void change(ItsPrivate other, int val) {

other.i = val;

}

}

}

produces:

It's 2

It's 3

paulcwa at 2007-7-8 21:44:06 > top of Java-index,Java Essentials,New To Java...
# 2
> If it would be true it violates the encapsulation> principle of OO.Well, the implementation of two objects of the same class is of course "tightly coupled", but couldn't ever differ. How would you otherwise implement equals() methods?
CeciNEstPasUnProgrammeura at 2007-7-8 21:44:06 > top of Java-index,Java Essentials,New To Java...
# 3
How does it violate the encapsulation principle? It's not only allowed, but even necessary sometimes. For example when you implement the "equals" and "compareTo" methods, it can be impractical or even impossible to use only the publicly available interface.
jsalonena at 2007-7-8 21:44:06 > top of Java-index,Java Essentials,New To Java...
# 4
Too late, icicle. ;)
CeciNEstPasUnProgrammeura at 2007-7-8 21:44:06 > top of Java-index,Java Essentials,New To Java...
# 5

Thank you everybody for your explanations!

I can understand that access is allowed in Java for the sake of easier implementation of 'equals()' and 'compareTo()' methods.

For the rest, I am not convinced that it is good idea, anyway.

Class defines possible properties of the object with their domains. Instances have their own states. Every instance has its own state. It is a private state, hidden inside the entity.

I cannot change you mind, only directly manipulating with you brain aka surgery. This Java technique reminds me such a surgery :)

Commona at 2007-7-8 21:44:06 > top of Java-index,Java Essentials,New To Java...
# 6

All kidding aside here - ask yourself: "Why would I want to make anything private in a class?". Then you might answer "Well, for one thing I do not want to be able to de-stabilize the class when I use it for what it is."

An example of such use is any use of the API classes from SUN. No instantiated Object from these has access to the private members, unless though public methods - which control such access.

But you might ask: "And why would I want to take that ability away from the class itself?" Well: "In answer you maight say that you would not want to, and continue by saying that's not what you mean."

Well since one would need to build in that capability - the previous example used an inner class with a change method - this is not the same as allowing outside access. If you tried to do something like from another toplevel public class without any public alteration methods in the member class you couldn't do it - you'd get a "x has private access in y" type message.

~Bill

.

abillconsla at 2007-7-8 21:44:06 > top of Java-index,Java Essentials,New To Java...
# 7

> If it would be true it violates the encapsulation

> principle of OO.

I think it strengthens encapsulation.

It's a common feature of many classes to perform operations involving two or more objects of the class. The fact that different objects of the same class can access each others private state directly is important for encapsulation because otherwise the state would have to be given higher visibility and become exposed outside the class (at least to the default access level).

So this feature of Java rather helps hiding private state which is what encapsulation is all about.

abillconsla at 2007-7-8 21:44:06 > top of Java-index,Java Essentials,New To Java...
# 8

> If it would be true it violates the encapsulation

> principle of OO. Can you please explain this

> question/answer because it is really confusing me.

So what you are saying is "I don't understand this answer because I want it to be something else. I don't like it."

Well, that's a bad attitude. Wishful thinking is only going to get in your way in the computer programming business, and you need to avoid it. That isn't an easy thing to do, but you have to do it. Otherwise you'll be looking for the problems where you want them to be and not where they are.

Now, it's perfectly fine to discuss whether Java's implementation of private is good OO or not. But it is what it is, and you have to accept that first.

DrClapa at 2007-7-8 21:44:06 > top of Java-index,Java Essentials,New To Java...
# 9

Thanks for your good attitute and effords!

Please, take a look at the following example:

template <class myType>

myType GetMax (myType a, myType b) {

return (a>b?a:b);

}

That is how you compare objects in C++. The main difference is that C/C++ use pointers, which are not presented in Java. Instead you get references.

It has a lof of implications like the one above. Indeed, it is not good or bad, it is just a design decision that has benifits and drawbacks as always.

I came from C/C++ world so some features from time to time puzzles me.

Thanks for everybody again!

Message was edited by:

Common

Commona at 2007-7-8 21:44:06 > top of Java-index,Java Essentials,New To Java...
# 10
what's the difference between pointer and reference ?i think it's the same in java we deal with references which is more easy thean pointers in c or c++
eaajea at 2007-7-8 21:44:07 > top of Java-index,Java Essentials,New To Java...
# 11
> The main> difference is that C/C++ use pointers, which are not> presented in Java. Instead you get references.How does your example show the use of pointers in C++?Java reference types are best compared with (very restricted) pointer types in C++.
eaajea at 2007-7-8 21:44:07 > top of Java-index,Java Essentials,New To Java...
# 12

> what's the difference between pointer and reference ?

You have to be careful when making cross-language comparisons. For me, the essense of a pointer -- what makes a pointer a pointer -- is being able to write the followng:

//classic!

while ((*p++ = *q++) != 0)

;

Java lacks pointer arithmetic :-(

DrLaszloJamfa at 2007-7-8 21:44:07 > top of Java-index,Java Essentials,New To Java...
# 13
It's kind of funny ... I started learning Java before having to learn C for my job. Thus when I program in C, I tend to shy away from pointers if I can. I don't know, maybe you'll think that's not funny, but ... !Bill ... Bill has left the building.
abillconsla at 2007-7-8 21:44:07 > top of Java-index,Java Essentials,New To Java...