Originally posted by marten kay:
...should you, and if so how, encapsulate the getText() in Line so that only the Document can be its client...
Strings are immutable, so if you simply "return text," that reference cannot be used to change the
String referenced by the private variable "text."
If you were dealing with a mutable object, then one approach is to create a copy (clone) of the object and return a reference to that copy, so that the original cannot be altered.
(I think this is a little advanced for the Beginners forum, so I'm moving this to the Intermediate forum. Please continue there.
)