Encapsulation vs Data Hiding - Java

前端 未结 19 2070
耶瑟儿~
耶瑟儿~ 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条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-04 08:57

    Data hiding means we are providing security to data within the class.

    Abstraction means hiding the code by defining member functions.

    Encapsulation is the combination of abstraction and data hiding, means we are wrapping data and code associated with that data. For ex bean class

        class student {
    private int age;  // Data Hiding 
    public setAge(){} // Abstraction 
    public getAge(){} // Abstraction 
    }
    

    student class is encapsulated.

    encapsulation = data hiding + abstraction

提交回复
热议问题