Answer the question
In order to leave comments, you need to log in
Why private properties in classes if getters and setters without logic are created for them?
I often come across the fact that a class has private properties and a getter and setter are created for each without any additional logic.
What is it for? Why not just leave public, since the property can be read and edited anyway?
Why do we need encapsulation for the sake of encapsulation?
Answer the question
In order to leave comments, you need to log in
Here is a good answer.
https://dzone.com/articles/why-should-i-write-gett...
This practice, as far as I remember, began to be applied in Java, where classes do not have properties, but only fields.
As a result, to redefine the logic and simplify refactoring: Removed one field, added another - the public interface did not break, because setters / getters can be corrected in one place, rather than to correct the logic in all places where the fields are twitching.
If the language has properties, then yes, this does not make much sense, because are themselves emulation properties.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question