Encapsulation vs Data Hiding - Java

前端 未结 19 2088
耶瑟儿~
耶瑟儿~ 2020-12-04 08:29

Interviewer: What is encapsulation and how do you achieve it in Java?

Me: Encapsulation is a mechanism to hide

19条回答
  •  Happy的楠姐
    2020-12-04 08:45

    1. ENCAPSULATION is the bundling the data (fields) and behavior (methods) together in classes.
    2. Now, You have to provide access control on the data using access modifiers (private/protected/public or default) which is DATA HIDING. It protects data from the outside world where you do not want to provide the access.
    3. Also, in order to hide the complexity of the operations you must use interfaces in order to achieve ABSTRACTION.

提交回复
热议问题