Difference between Encapsulation and Abstraction

后端 未结 15 1120
萌比男神i
萌比男神i 2020-12-07 07:21

I had an interview today. I had a question from OOP, about the difference between Encapsulation & Abstraction?

相关标签:
15条回答
  • 2020-12-07 07:59

    Abstraction

    In Java, abstraction means hiding the information to the real world. It establishes the contract between the party to tell about “what should we do to make use of the service”.

    Example, In API development, only abstracted information of the service has been revealed to the world rather the actual implementation. Interface in java can help achieve this concept very well.

    Interface provides contract between the parties, example, producer and consumer. Producer produces the goods without letting know the consumer how the product is being made. But, through interface, Producer let all consumer know what product can buy. With the help of abstraction, producer can markets the product to their consumers.

    Encapsulation:

    Encapsulation is one level down of abstraction. Same product company try shielding information from each other production group. Example, if a company produce wine and chocolate, encapsulation helps shielding information how each product Is being made from each other.

    1. If I have individual package one for wine and another one for chocolate, and if all the classes are declared in the package as default access modifier, we are giving package level encapsulation for all classes.
    2. Within a package, if we declare each class filed (member field) as private and having a public method to access those fields, this way giving class level encapsulation to those fields
    0 讨论(0)
  • 2020-12-07 07:59

    Let me explain it in with the same example discussed above. Kindly consider the same TV.

    Encapsulation: The adjustments we can make with the remote is a good example - Volume UP/DOWN, Color & Contrast - All we can do is adjust it to the min and max value provided and cannot do anything beyond what is provided in the remote - Imagine the getter and setter here(The setter function will check whether the value provided is valid if Yes, it process the operation if not won't allow us to make changes - like we cannot decrease the volume beyond zero even we press the volume down button a hundred times).

    Abstraction: We can take the same example here but with a higher Degree/Context. The volume down button will decrease the volume - and this is the info we provide to the user and the user is not aware of neither the infrared transmitter inside the remote nor the receiver in the TV and the subsequent process of parsing the signal and the microprocessor architecture inside the TV. Simply put it is not needed in the context - Just provide what is necessary. One can easily relate the Text book definition here ie., Hiding the inner implementation and only providing what it will do rather than how it do that!

    Hope it clarifies a bit!

    0 讨论(0)
  • 2020-12-07 08:03

    Encapsulation: Wrapping code and data together into a single unit. Class is an example of encapsulation, because it wraps the method and property.

    Abstraction: Hiding internal details and showing functionality only. Abstraction focus on what the object does instead of how it does. It provides generalized view of classes.

    int number = 5;
    string aStringNumber = number.ToString(); 
    

    Here, ToString() is abstraction. And how this mechanism number variable converted to string and initialize into aStringNumber is encapsulation.

    Let us take a real world example of calculator. Encapsulation is the internal circuits, battery, etc., that combine to make it a calculator. Abstraction is the different buttons like on-off, clear and other buttons provided to operate it.

    0 讨论(0)
  • 2020-12-07 08:06

    Encapsulation:

    Hiding something, sort of like medicine capsule. We don't know what is in the capsule, we just take it. Same as in programming - we just hide some special code of method or property and it only gives output, same as capsule. In short, encapsulation hides data.

    Abstraction:

    Abstraction means hiding logic or implementation. For example, we take tablets and see their color and but don't know what is the purpose of this and how it works with the body.

    0 讨论(0)
  • 2020-12-07 08:06

    Just a few more points to make thing clear,

    One must not confuse data abstraction and the abstract class. They are different.

    Generally we say abstract class or method is to basically hide something. But no.. That is wrong. What is the word abstract means ? Google search says the English word abstraction means

    "Existing in thought or as an idea but not having a physical or concrete existence."

    And thats right in case of abstract class too. It is not hiding the content of the method but the method's content is already empty (not having a physical or concrete existence) but it determines how a method should be (existing in thought or as an idea) or a method should be in the calss.

    So when do you actually use abstract methods ?

    • When a method from base class will differ in each child class that extends it.
    • And so you want to make sure the child class have this function implemented.
    • This also ensures that method, to have compulsory signature like, it must have n number of parameters.

    So about abstract class! - An Abstract class cannot be instantiated only extended! But why ?

    • A class with abstract method must be prevented from creating its own instance because the abstract methods in it, are not having any meaningful implementation.
    • You can even make a class abstract, if for some reason you find that it is meaning less to have a instance of your that class.

    An Abstract class help us avoid creating new instance of it!

    An abstract method in a class forces the child class to implement that function for sure with the provided signature!

    0 讨论(0)
  • 2020-12-07 08:06

    Briefly, Abstraction happens at class level by hiding implementation and implementing an interface to be able to interact with the instance of the class. Whereas, Encapsulation is used to hide information; for instance, making the member variables private to ban the direct access and providing getters and setters for them for indicrect access.

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