Re:Ask HN: Why do you make class members private?
by sshine on Monday July 25, 2022 @04:39PM
(#32230595)
> Encapsulation is a founding principle of OOP.
The more fundamental principle that predates OOP is: Abstraction
https://en.wikipedia.org/wiki/Abstraction_(computer_science)
The practical reason is that this way you have limited paths (methods) with which the inner workings of a class can be modified with, whereas if anything would be public, any other obejct would be able to change a class' status rendering debugging incredibly more complicaed.
This is also a part of the Open-Closed principle: "software entities (classes, modules, functions, etc.) should be open for extension, but closed for modification" https://en.wikipedia.org/wiki/Open%E2%80%93closed_principle