Course Object Oriented Programming

Geters and Setters

Course's Page 16 min Text by
User Image
Abacate

Hello, in this class we will understand the methods getters and the methods setters.

Good visibility practice

When building a class, a good practice is to leave the attributes as private and only the methods as public. This prevents attributes from being used other than through the methods already established.

But how to access the attributes if they cannot be accessed?

  • Creating methods to return the value (Getters).
  • Creating methods to change the value (Setters).

Get Method

Methods known as Getters are methods used to "take" the value of an attribute.

A Get method is related to its use, and the name Get is not required in the method name. However, it is good practice to use Get in the name of the method to make it clear what it is intended to do.

Get example