Encapsulation vs Data Hiding - Java

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

    In the class, attributes and behaviors are placed together. An encapsulation means, that when you create a class, this itself means that you realize encapsulation principle. Because data and behaviors are encapsulated into the class. So data hiding is a major part of the term - encapsulation. And this means access points of the object.

    0 讨论(0)
  • Encapsulation means binding data members and method(behaviour) in a single unit and providing as much information as much user required easily you can say [encapsulation=data hiding+abstraction].

    Class is fully encapsulated element in java

    Data hiding means declaring data variables as private to secure it from unauthorized access but if you are giving some public setter and getter method for validation.

    0 讨论(0)
  • 2020-12-04 09:00

    Data encapsulation refers to the mechanism of keeping all the related properties and method into a single entity.

    Example: A Car. It keeps steering wheels, tires, engine and all the related stuffs into one collective entity known as Car.

    In programming languages the encapsulation is achieved with classes. A class contains all the properties and related methods in one single entity that is designed to do a specific task.


    Data hiding refers to hiding the unimportant details from the user and show him only the relevant data.

    Example: When we press brakes or accelerator in a car, we don't know what's happening behind the scene (how its increases its speed or how its applying the brakes into tires). All we know is that we have brake and accelerator to perform the desired outcome.

    In programming this is done with the help of access modifiers. Private members are not accessible from outside the class and only public members are accessible to users. The private members can only be accessed from members of the class thus providing the security for private members to be directly evaluated from the outside of the class.

    0 讨论(0)
  • 2020-12-04 09:00

    Encapsulation: Encapsulation is one of the pillar of OOPS, also is superset for the other two terms, data hiding and abstraction. (Data hiding, as the term refers.. Data can only be stored in variables. To hide the data from external world we use data hiding concept. Public, Private, Protected these are three access modifiers provided to us for data hiding. Default is limited to package. Protected limited to inherited class.) ** Private variables are accessible only inside class which means if there is an inner class, there also private variables could be called directly. Only Static Inner Classes can call Static Variables.

    Now coming to Abstraction (meaning Intangible). Abstraction is also considered as the process to hide the implementation from user and provide only final result. Example: we have a person class which has “Weight” property. This property is quite tricky because we cannot accept less than 0 weight for a person. Also we cannot accept more than 500 kgs. So hiding the implementation for these will be in the setter method. This thing hiding the implementation details is Abstraction.

    ** And the Complete Package i.e. making the object private and implementation of logic in setter and getter is called ENCAPSULATION.

    0 讨论(0)
  • 2020-12-04 09:02

    From Wikipedia

    Encapsulation Encapsulation is an object-oriented programming concept that binds together the data and functions that manipulate the data, and that keeps both safe from outside interference and misuse. Data encapsulation led to the important OOP concept of data hiding.

    If a class does not allow calling code to access internal object data and permits access through methods only, this is a strong form of abstraction or information hiding known as encapsulation. Some languages (Java, for example) let classes enforce access restrictions explicitly, for example denoting internal data with the private keyword and designating methods intended for use by code outside the class with the public keyword. Methods may also be designed public, private, or intermediate levels such as protected (which allows access from the same class and its subclasses, but not objects of a different class). In other languages (like Python) this is enforced only by convention (for example, private methods may have names that start with an underscore). Encapsulation prevents external code from being concerned with the internal workings of an object. This facilitates code refactoring, for example allowing the author of the class to change how objects of that class represent their data internally without changing any external code (as long as "public" method calls work the same way). It also encourages programmers to put all the code that is concerned with a certain set of data in the same class, which organizes it for easy comprehension by other programmers. Encapsulation is a technique that encourages decoupling.

    0 讨论(0)
  • 2020-12-04 09:04

    Encapsulation

    In general Encapsulation means to bundle similar items, simple as that.

    For example, take a Student class where we will have students instance variables and behaviors/methods acting on those instance variables @ one place.

    Why it is important? We don't want our code scattered all around in our code Base.

    If let say we need to make changes then we need find the variants(of that changes) at all the places. By Bundling similar items we are just avoiding such scenarios and it also helps to make our Bundled code more focused.

    Data Hiding

    It just provides a way to protect your data from the outside world. What it means is, lets say if I made my instance variable public, then anyone can change its state. But if we make our instance variable private/protected then actually we are restricting outside entities from making changes to it.

    Comparison/Discussion

    Now question arises, in what respect are we making our variables protected?

    Again we need to understand that Encapsulation is just the container we need to place our similar items.

    Its just act like a Black box to outside world. Outside world (I mean to say clients/consumers: using our Student class) don't know the internal details/implementation details of the Student class and actually they should not care about the internal details/implementation details of the class. They just want some methods/APIs so that they can use them in their client application.

    So my point is all student related behaviors/variables are placed in a Black box which we are calling it as a class. Now its up to designer of the class what are the elements of the class should be hidden and what should not be hidden from outside world.

    Now coming back to the question In Java: we are making our variables private it means they are class protected.If we want our instance variables to be accessible throughout the package then they are package protected. Through out project they are public. So what I mean to say is to hide data you need some sort of container where you will put your data and hide with respect to container.

    So what I feel Data Hiding is not possible without Encapsulation. You can't hide your data without putting it in some form of container. Again I'm reminding you that I'm putting this on the Object Oriented Language context.

    But yes Encapsulation is possible without hiding your data. Put all things public and you can the see the affect.

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