Why do we need to protect attributes? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Why do we need to protect attributes?

Why do we protect attributes in classes? From what exactly? I am the one who writes the code and I can decide if I'm gonna use an attribute and how. How is that even a protection?

10th Jun 2019, 10:09 AM
Malika Oubilla
Malika Oubilla - avatar
1 Answer
+ 3
'never trust in input' Protecting attributes by using getters and setters allow you to check the datas before modifying an attribute. It allows too to have read-only attributes.
10th Jun 2019, 10:14 AM
Théophile
Théophile - avatar