+ 5

Encapsulation

If the variables of one class are hidden from other classes, and accessible only through the methods of the current class, why are PUBLIC setter methods provided?(it can modify the variable from outside the class then, which makes hiding the variable rather useless??)

3rd Jan 2018, 7:32 AM
DPPPPPPP
1 Answer
- 5
You realized something good, just because it's valid, doesn't mean it's a good practice! To keep things modular, it's good practice to never declare a setter as public, and your setters should validate data before inserting them into a variable, this way your business rules doesn't get messed up
20th Sep 2019, 6:31 PM
Eduardo Bulsoni
Eduardo Bulsoni - avatar