New course! Every coder should learn Generative AI!
Try a free lesson0
setters are methods used to set the values of a class variable in a controlled manner.
Imagine if you have a Person class with age property.
You asked a user to enter his/her age .
age cannot be a negative number.
So you set it with a setter method and handle the exception within the setter method, if the user enters an invalid value/input.