Encapsulation vs Data Hiding - Java

前端 未结 19 1908
耶瑟儿~
耶瑟儿~ 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:38

    The easiest definition of encapsulation is "Binding data and code that operates data into a single unit", so that the data can't be accessed directly by outside world.

    In structure language like c data is declared at the start of the block and used directly, accessed openly(Any tom, dick and harry can access data). Data hiding is a concept to restrict data access directly and openly to any tom dick and harry.
    Encapsulation is one of the way to achieve Data hiding. You achieve data hiding by putting data and function together (Encapsulation).

    How you achieve in JAVA, see any getter(), setter() function example in JAVA. Check this

    How do getters and setters work?

    0 讨论(0)
  • 2020-12-04 08:40

    Abstraction says that what needed to interact with object.

    Encapsulation means encapsulate object properties , state and behaviours into single logical unit called class.

    Data hiding says that hiding inner implementation of objects means private methods and properties, the object that use to maintain their state.

    0 讨论(0)
  • 2020-12-04 08:43

    Encapsulation: Take the example of a capsule. If you open it, it contains a lot of ingredients in it. Object Oriented programming's Encapsulation is also like that. As the name suggests "Encapsulation means to encapsulate (make a capsule of) all the data members, attributes and corresponding methods within one single capsule.

    How do you do it: Lets say you made a class named "Car". Now car has a color price and model. These are attributes, and it has a run method. So here you've encapsulated all these attributes and methods of a vehicle named "Car". When you create an instance of car like so

    Car myCar = new Car();
    

    You can access all the attributes of Car using the myCar variable.

    "Data hiding": Data hiding is controlled in Java using access modifiers. To access the data members you use ACCESSORS while to modify the data you use "Mutators". Java doesn't provide accessors and mutators by itself, you create them yourself (getters and setters). While C# provides PROPERTIES to do so.

    0 讨论(0)
  • 2020-12-04 08:44

    More generally encapsulation refers simply to bundling the data (e.g. of an object) with the operations on that data. So you have a class encapsulating data - fields - along with the methods for manipulating that data.

    But encapsulation is also sometimes used in the same way as your answer, and indeed, one of the points of bundling data and methods is to hide the implementation.

    I suppose a better answer than just use methods and make all fields private is: use interfaces. This way, operations on an object are purely based on the interface contract, and are in no way tied to the fields or helper methods used to implement that contract internally.

    0 讨论(0)
  • 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.
    0 讨论(0)
  • 2020-12-04 08:47

    I hate to do this but, from Wikipedia:

    In programming languages, encapsulation is used to refer to one of two related but distinct notions, and sometimes to the combination thereof:

    1. A language mechanism for restricting access to some of the object's components.
    2. A language construct that facilitates the bundling of data with the methods (or other functions) operating on that data

    Your explanation was more along the lines of the first notion, the interviewer was looking for something on the second.

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