What is the purpose of using getters and setters in Java?

  • To access and modify the private attributes
  • To create objects
  • To declare variables
  • To perform mathematical operations
Getters and setters are used to access and modify the private attributes (variables) of a class. They help in achieving encapsulation by providing controlled access to the class's internal state. Getters allow reading the value, and setters allow modifying it while enforcing rules and validation.
Add your answer
Loading...

Leave a comment

Your email address will not be published. Required fields are marked *