Forums Register Login

Dan's question, OO programming

+Pie Number of slices to send: Send
A class can not be called "tightly encapsulated" unless which of the following is true?
a. The class is declared final.
b. All local variables are declared private.
c. All method parameters are declared final.
d. No method returns a reference to any object that is referenced by an internal data member.
e. None of the above
Answer : E. THe explanation is "However, the methods of a tightly encapsulated class may return a reference to an immutable object or a reference to a copy or clone of an internal object.". I don't quite understand to this sentence. What is "a reference to a copy or clone of an internal object"? And reference to an immutable object, does it mean to a String object?
Thanks
David
+Pie Number of slices to send: Send
David,
The following code demonstrates what can happen if you allow an object to return a reference to an internal object that is not immutable.

The program output is as follows.
ABC
ABCXYZ
That problem can be avoided if the getIt method returns a reference to a copy of the StringBuffer instance rather than a reference to the same StringBuffer instance that is a member of class A.
+Pie Number of slices to send: Send
Hi Dan,
Thanks for the reply. I am now understand ...
Thanks
You may have just won ten million dollars! Or, maybe a tiny ad.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 674 times.
Similar Threads
Tightly encapsulated classes
Encapsulation-- Dan's Mock Exam
Encapsulation
What constitutes encapsulation?
Encapsulation??
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 02:20:11.