Confused with Java Encapsulation Concept

后端 未结 6 1718
抹茶落季
抹茶落季 2021-01-03 06:37

Good day!

I am reading a Java book about encapsulation and it mentioned the getter and setter method.

I\'ve read that to hide the attributes, I must mark my

6条回答
  •  梦毁少年i
    2021-01-03 07:25

    I agree with all the great answers provided. I would like to add another great benefit about using encapsulation and is the fact that by hiding the data from its clients (just as the examples described above) you can guarantee how to set and get these instance variables making your class easier to maintain and test (i.e if you have many clients manipulating your objects variables you don't need to go class by class writing the new functionality, instead you just modify the getter and setter).

    By making usage only of the objects setters and getters, the details of its implementation can be hidden from the outside world. This is a nice advantage so other objects attempting to interact with your class won't be able to break its functionality. This also increases the likelihood that a system will be implemented effectively.

提交回复
热议问题