What's the difference between abstraction and encapsulation?

前端 未结 24 1020
攒了一身酷
攒了一身酷 2020-12-22 17:16

In interviews I have been asked to explain the difference between abstraction and encapsulation. My answer has been along the lines of

  • Abstraction<

相关标签:
24条回答
  • 2020-12-22 17:56

    Its Simple!

    Take example of television - it is Encapsulation, because:

    1. Television is loaded with different functionalies that i don't know because they are completely hidden.

    2. Hidden things like music, video etc everything bundled in a capsule that what we call a TV

    Now, Abstraction is When we know a little about something and which can help us to manipulate something for which we don't know how it works internally.

    For eg: A remote-control for TV is abstraction, because

    1. With remote we know that pressing the number keys will change the channels. We are not aware as to what actually happens internally. We can manipulate the hidden thing but we don't know how it is being done internally.

    Programmatically, when we can acess the hidden data somehow and know something.. is Abstraction .. And when we know nothing about the internals its Encapsulation.

    Without remote we can't change anything on TV we have to see what it shows coz all controls are hidden.

    0 讨论(0)
  • 2020-12-22 17:58

    In my view encapsulation is a thought of programmer to hide the complexity of the program code by using access specifier.
    Where as Abstraction is separation of method and object according to there function and behavior. For example Car has sheets, wheels, break, headlight.

    0 讨论(0)
  • 2020-12-22 17:59

    Encapsulation

    Encapsulation from what you have learnt googling around, is a concept of combining the related data and operations in a single capsule or what we could say a class in OOP, such that no other program can modify the data it holds or method implementation it has, at a particular instance of time. Only the getter and setter methods can provide access to the instance variables.

    Our code might be used by others and future up-gradations or bug fixes are liable. Encapsulation is something that makes sure that whatever code changes we do in our code doesn't break the code of others who are using it.

    Encapsulation adds up to the maintainability, flexibility and extensibility of the code.

    Encapsulation helps hide the implementation behind an interface.

    Abstraction

    Abstraction is the process of actually hiding the implementation behind an interface. So we are just aware of the actual behavior but not how exactly the think works out internally. The most common example could the scenario where put a key inside the lock and easily unlock it. So the interface here is the keyhole, while we are not aware of how the levers inside the lock co-ordinate among themselves to get the lock unlocked.

    To be more clear, abstraction can be explained as the capability to use the same interface for different objects. Different implementations of the same interface can exist, while the details of every implementation are hidden by encapsulation.

    Finally, the statement to answer all the confusions until now - The part that is hidden relates to encapsulation while the part that is exposed relates to abstraction.

    Read more on this here

    0 讨论(0)
  • 2020-12-22 18:00

    Abstraction: In case of an hardware abstraction layer, you have simple interfaces to trigger the hardware (e.g. turn enginge left/right) without knowing the hardware details behind. So hiding the complexity of the system. It's a simplified view of the real world.

    Encapsulation: Hiding of object internals. The object is an abstraction of the real world. But the details of this object (like data structures...) can be hidden via encapsulation.

    0 讨论(0)
  • 2020-12-22 18:01

    Encapsulation is used for 2 main reasons:

    1.) Data hiding & protecting (the user of your class can't modify the data except through your provided methods).

    2.) Combining the data and methods used to manipulate the data together into one entity (capsule). I think that the second reason is the answer your interviewer wanted to hear.

    On the other hand, abstraction is needed to expose only the needed information to the user, and hiding unneeded details (for example, hiding the implementation of methods, so that the user is not affected if the implementation is changed).

    0 讨论(0)
  • 2020-12-22 18:04

    I know there are lot's of answers before me with variety of examples.
    Well here is my opinion abstraction is getting interested from reality .

    In abstraction we hide something to reduce the complexity of it and In encapsulation we hide something to protect the data.

    So we define encapsulation as wrapping of data and methods in single entity referred as class.

    In java we achieve encapsulation using getters and setters not just by wrapping data and methods in it. we also define a way to access that data. and while accessing data we protect it also.

    Techinical e.g would be to define a private data variable call weight.Now we know that weight can't be zero or less than zero in real world scenario.
    Imagine if there are no getters and setters someone could have easily set it to a negative value being public member of class.

    Now final difference using one real world example,
    Consider a circuit board consisting of switches and buttons. We wrap all the wires into a a circuit box, so that we can protect someone by not getting in contact directly(encapsulation).

    We don't care how those wires are connected to each other we just want an interface to turn on and off switch. That interface is provided by buttons(abstraction)

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