Encapsulation vs Data Hiding - Java

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

    The question appears misleading, I doubt that anybody but the interviewer could possibly answer it and chances are that his/her answer could be wrong. Most importantly, it is not clear what the question is supposed to evaluate or discover.

    Still, I gave it some thought and the following is what I believe could be my attempt to answer the question.

    With Access Modifiers

    In Java, encapsulation is implemented by hiding details using the accessibility modifiers (i.e. public, protected, private, etc.). With these levels of accessibility you control the level of information hiding. The less restrictive the level, the more expensive change is when it happens and the more coupled the class is with other dependent classes (i.e. user classes, subclasses).

    Evidently, encapsulation is more than just hiding state. In Java you can hide entire classes and interfaces and their state and behaviors, by this, hiding the implementation details of an entire API.

    For example, the method Arrays.asList() returns a List implementation, but we do no care which implementation, as long as it satisfies the List public interface, right?. The implementation can be changed in the future without affecting us, the users of the method, but the actual implementation is hidden from us.

    Up to this point, it would appear as if encapsulation depended entirely from the capacity of a programming language to hide details, and therefore, that it could not be achieved without access modifiers, right?

    Without Access Modifiers

    However, how does a language like Python achieves encapsulation when it does not have access modifiers? Everything is public in Python? Does that mean that encapsulation is not possible?

    What if, by convention, we define the public interface of our components and then access the objects' state and behavior only through their public interface? Evidently, for this we need a clear understanding of the abstractions in our problem domain and how these are supposed to be consumed by our users.

    To me, it appears as if the interview question was intended to evaluate encapsulation as broader concept, one depending on the definition of very clear abstractions, and not just a as by-product of a language feature like access modifiers.

    The Role of Abstraction

    That's why, in my opinion, to really understand encapsulation, one must first understand abstraction.

    Think, for example, in the level of abstraction in the concept of a car. A car is complex in its internal implementation. They have several subsystem, like a transmission system, a break system, a fuel system, etc.

    However, we have simplified its abstraction, and we interact with all cars in the world through the public interface of their abstraction. We know that all cars have a steering wheel through which we control direction, they have a pedal that when you press it you accelerate the car and control speed, and another that when you press it you make it stop, and you have a gear stick that let you control if you go forward of backward. These features constitute the public interface of the car abstraction. In the morning you can drive a sedan and then get out of it and drive an SUV in the afternoon as if it was the same thing.

    It is not that you cannot open the hood and see how it all works. However, few of us know the details of how all these underlying features are implemented under the hood, and the truth is that we do not need to know the details to drive a car. All these things are encapsulated under the car abstraction. We only need to know the public interface of the abstraction.

    Think of the time when cars did not have a hydraulics directional system. One day, the car manufactures invented it, and they decide it to put it in cars from there on. Still, this did not change the way in which users where interacting with them. At most, users experienced an improvement in the use of the directional system. A change like this was possible because the internal implementation of a car was encapsulated.

    This clearly demonstrates that by hiding the details of how the directional systems was implemented, they could safely change it without affecting the public interface of the car and, correspondingly, without affecting the way users interacted with it.

    Now, think that car manufactures decided to put the fuel cap below the car, and not in one of its sides. You go and buy one of these new cars, and when you run out of gas you go to the gas station, and you do not find the fuel cap. Suddenly you realize is below the car, but you cannot reach it with the gas pump hose. Now, we have broken the public interface contract, and therefore, the entire world breaks, it falls apart because things are not working the way it was expected. A change like this would cost millions. We would need to change all gas pumps in the world. When we break encapsulation we have to pay a price.

    So, as you can see, the goal of encapsulation is to minimize interdependence and facilitate change. You maximize encapsulation by minimizing the exposure of implementation details. The state of a class should only be accessed through its public interface.

    The beauty of encapsulation is the power of changing things without affecting its users.

    We could say that this ultimate goal depends on careful planning and design. In Java, access modifiers are a way to bring these ideas to life, but in language where this feature does not exist, it is equally possible to achieve encapsulation.

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

    The process of binding data members and corresponding methods into a single unit is nothing but encapuslation.

    Encapsulation = Data Hiding + Abstraction

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

    Basically Data encapsulation is a process while data hiding is a technique . In encapsulation data can be public or private but in data hiding data is only private. So data hiding uses Encapsulation but it also uses different techniques to keep data permitted to authorized access only. Hope it helps!

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

    The way i see it , Encapsulation refer to the idea of binding data and method to a single unit called class . However there are two main criteria to bind data and methods . One of them is information hiding, just not the only one . To be perfectly concise i would say the two main criteria are

    1. Low Coupling (ensure by information hiding)
    2. High Cohesion
    0 讨论(0)
  • 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

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

    The process of defining a class by hiding its data from direct access from the outside class member and providing the access only through publicly accessible setter and getter method through proper validation it is called Encapsulation.

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