0

This is just a form question.

Using Java, why when trying to return a value from an object do you use object.getData() from a defined getData() function within the class instead of simply using object.data.

They both seem to return the same thing except that object.getData() is more typing and requires you to define a function in the object unnecessarily.

TheSaint321
  • 390
  • 2
  • 4
  • 17
  • `public Foo getFoo() { return MethodToCreateDeepCopyOfFoo(); }` for example will allow you to return data while preventing the caller from altering the state of the original object. It's all about information hiding and information control. – Hovercraft Full Of Eels Apr 26 '17 at 17:32
  • one enables the owner of the **attribute** to determine how its data is **accessed/modified** whereas the other completely ignores the concept of **encapsulation**. – Ousmane D. Apr 26 '17 at 17:33

0 Answers0