Why use getters and setters/accessors?
Getters and setters, also known as accessors, are methods that are used to get and set the values of an object's properties.
There are several reasons why you might want to use getters and setters:
Encapsulation: Getters and setters allow you to hide the implementation details of a class and expose a simplified interface to the outside world. This helps to protect the internal state of the object from being modified in unexpected ways.
Flexibility: Getters and setters give you the flexibility to change the implementation of a property without breaking code that depends on it. For example, you might want to add validation logic or perform some other operation when a property is set.
Code reusability: Getters and setters can be reused in multiple classes, which can save you time and reduce the risk of introducing bugs when you need to access or modify a property.
Code readability: Getters and setters can make your code more readable by giving names to the properties that you are accessing or modifying. This can make it easier to understand what your code is doing, especially if you are working with complex data structures.
I hope this helps! Let me know if you have any questions.