DEV Community

Discussion on: Encapsulate state and expose behavior when writing object-oriented code

Collapse
 
pnunezcalzado profile image
Pedro Nuñez Calzado

I'm not a Software Engineer, just a building architect with some programming knowledge. It so interesting the topic of the wrong use of the languages.

In my mind, the getter and the setter are enough to provide encapsulation, because the only "forbidden" thing in OOP is to make public fields.

Once you do both a getter and a setter in a public property, you are effectively isolating the private field from direct accessing, and you can leave those empty or implement the code you need to control the access.

In the case of the auto property, the compiler creates for you the private field. The concept of private setter is needed to provide access to the property within the class.