Win a copy of The Way of the Web Tester: A Beginner's Guide to Automating Tests this week in the Testing forum!

# Encapsulation (was useless: Am i right!)

Xiao Song
Greenhorn
Posts: 17

Which statement is true?
A. The Mycircle class is fully encapsulated.
B. The diameter of a given MyCircle is guaranteed to be twice its radius.
C. Lines 6 and 7 should be in a synchronized block to ensure encapsulation.
D. The radius of a MyCircle object can be set without affecting its diameter.

I think the right answer is D, am I right?
thanks advance!
[ November 24, 2006: Message edited by: Barry Gaunt ]

Abdi Duale
Greenhorn
Posts: 7
I am convinced that the answer is B. A is false because the class is not encapsulated as the members and the class are left public. C putting those two lines in a block won't encapsulate, I don't see how it would. D, once the radius is set, the diameter is obvious from the formula.

Best of luck!

Gian Franco
blacksmith
Ranch Hand
Posts: 979
I think the answer is D as well.

(A) False, MyCircle is not fully encapsulated, because it's instance variables are public and not private

(B) False, The diameter is not guaranteed to alway be twice it's radius, because the diameter can be altered by altering it's public variable

(C) False, Synchronization doesn not play a role in encapsulation in general, and encapsulation will still be broken by the above

(D) True, since the instance variable radius is public it can be accessed and altered without affecting the diameter.

Kind Regards,

Gian

Abdul Rehman
Ranch Hand
Posts: 65
A. The MyCircle class is fully encapsulated.

Totally wrong. The class members are public and therefore, not encapsulated.

B. The diameter of a given MyCircle is guaranteed to be twice its radius.

Again wrong. The members are public and therefore, open to abrupt change. Moreover, no constructor means that the radius == diameter for a newly constructed object. Thus there is no guarantee at all.

C. Lines 6 and 7 should be in a synchronized block to ensure encapsulation.

The method setRadius() should be synchronized if it will be accessed by multiple threads simultaneously, to prevent a "data race". This is illustrated in the following two figures, taken from the JLS.

For explanation & details, see Section 17.4.5 in the JLS.

But still, this option isn't correct, because, this doesn't seem to be related with encapsulation.

Looking at this from another perspective, if the method setRadius() isn't synchronized & is accessed by multiple threads at the same time, it will result in disaster. The radius & diameter variables may get inconsistent/incorrect values due to this. Will this break/affect the encapsulation then? Anyone on this...

D. The radius of a MyCircle object can be set without affecting its diameter.

Yes, indeed. Both instance variables are public and therefore, can be changed in any manner whatsoever.

A & B are incorrect & D is correct, undoubtedly. I am unsure about option C.

Best regards,
Abdul Rehman.
[ November 24, 2006: Message edited by: Abdul Rehman ]