Say I have a class called Person
that has two attributes: age
and dateOfBirth
.
Now since I want the age
attribute to be changed according to the dateOfBirth
attribute, I will create a setDateOfBirth()
method that will set the dateOfBirth
attribute and also calculate and set the age
attribute.
My question is why should I make these attributes private
if I am (the programmer) the only one accessing the object directly and I know not to change them without accessing the methods. The most obvious reason I can think of is that I could change them by accident, and so making them private
will prohibit me from doing that, are there any other reasons for making the attributes private
?