[Solved] What are getters/setters good for? [duplicate]


Getters and setters are used in order to prevent code outwith the class from accessing implementation details. Maybe today some piece of data is just a string, but tomorrow it has be created by joining two other strings together and also keeping a count of the number of times the string is retrieved (OK, contrived example).

The point is that by forcing access to your class to go through methods, you’re free to change how your class does things without impacting other code. Public properties don’t give you that guarantee.

On the flip side, if all you want to do is hold data, then public properties are fine, but I think that’s a special case.

2

solved What are getters/setters good for? [duplicate]