What is the use of encapsulation when I'm able to change the property values with setter methods?

前端 未结 15 2042
情深已故
情深已故 2020-12-08 11:08

I try to understand a lot of times but I failed to understand this.

Encapsulation is the technique of making the fields in a class private and provi

15条回答
  •  -上瘾入骨i
    2020-12-08 11:39

    Any how i am able to change the values of fields through setter methods.

    Only if the setter method lets you do that.

    How we are preventing the accessing fields?

    The setter and getter get to control if and how you can access the fields.

    A setter may check if the value is valid. It may ask a SecurityManager if you should be allowed to do this. It may convert between data types. And so on.

提交回复
热议问题