In C++, I can define an accessor member function that returns the value of (or reference to) a private data member, such that the caller cannot modify that private
When you do something like this:
Object2 obj2 = obj1.getObj2();
obj2 = new Object2();
The original private member (obj1.obj2) remains as it were before (just to be sure that you grasped that concept). You can just omit the setter to obj2 so that the inner field cannot de changed.
If you want Object2 fields to be immutable you will need to apply the same pattern (private fields, no setters).
This answer your question?