What is encapsulation? How does it actually hide data?

后端 未结 7 1808
没有蜡笔的小新
没有蜡笔的小新 2020-12-05 17:11

Searching turns up a simple definition: data hiding.

But, consider the following two examples:

1) First Example:

Class Emp         


        
7条回答
  •  不知归路
    2020-12-05 17:33

    Encapsulation separates the concept of what something does from how it is implemented.

    Encapsulation is a very important concept in Object Oriented Programming. It is hiding the data from other modules in the application. In your example 2, as you can see you can only get the age and that too using a getter method, but you can only set the value to the private member within that class and not outside it. This is the advantage of Encapsulation.

提交回复
热议问题