What is encapsulation? How does it actually hide data?

后端 未结 7 1790
没有蜡笔的小新
没有蜡笔的小新 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:39

    In your first example the variable age is set as a public variable. Here anyone can modify the meeber age when an object of type Employee is declared.

    However, in your second example the variable age is actually private, and can only be modified from inside of the class Employee. This is the case of data hiding.

    In object oriented programming encapsulation is when one object is composed of another. This is in contrast to inheritance where an object can extent an already existing class.

    0 讨论(0)
提交回复
热议问题