Difference between Encapsulation and Abstraction

后端 未结 15 1121
萌比男神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 08:11

    If I am the one who faced the interview, I would say that as the end-user perspective abstraction and encapsulation are fairly same. It is nothing but information hiding. As a Software Developer perspective, Abstraction solves the problems at the design level and Encapsulation solves the problem in implementation level

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

    Abstraction - is the process (and result of this process) of identifying the common essential characteristics for a set of objects. One might say that Abstraction is the process of generalization: all objects under consideration are included in a superset of objects, all of which possess given properties (but are different in other respects).

    Encapsulation - is the process of enclosing data and functions manipulating this data into a single unit, so that to hide the internal implementation from the outside world.

    This is a general answer not related to a specific programming language (as was the question). So the answer is: abstraction and encapsulation have nothing in common. But their implementations might relate to each other (say, in Java: Encapsulation - details are hidden in a class, Abstraction - details are not present at all in a class or interface).

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

    Yes, it is true that Abstraction and Encapsulation are about hiding.

    • Using only relevant details and hiding unnecessary data at Design Level is called Abstraction. (Like selecting only relevant properties for a class 'Car' to make it more abstract or general.)

    • Encapsulation is the hiding of data at Implementation Level. Like how to actually hide data from direct/external access. This is done by binding data and methods to a single entity/unit to prevent external access. Thus, encapsulation is also known as data hiding at implementation level.

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

    There is a great article that touches on differences between Abstraction, Encapsulation and Information hiding in depth: http://www.tonymarston.co.uk/php-mysql/abstraction.txt

    Here is the conclusion from the article:

    Abstraction, information hiding, and encapsulation are very different, but highly-related, concepts. One could argue that abstraction is a technique that helps us identify which specific information should be visible, and which information should be hidden. Encapsulation is then the technique for packaging the information in such a way as to hide what should be hidden, and make visible what is intended to be visible.

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

    Abstraction: Is usually done to provide polymorphic access to a set of classes. An abstract class cannot be instantiated thus another class will have to derive from it to create a more concrete representation.

    A common usage example of an abstract class can be an implementation of a template method design pattern where an abstract injection point is introduces so that the concrete class can implement it in its own "concrete" way.

    see: http://en.wikipedia.org/wiki/Abstraction_(computer_science)

    Encapsulation: It is the process of hiding the implementation complexity of a specific class from the client that is going to use it, keep in mind that the "client" may be a program or event the person who wrote the class.

    see: http://en.wikipedia.org/wiki/Encapsulation_(object-oriented_programming)

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

    Encapsulate hides variables or some implementation that may be changed so often in a class to prevent outsiders access it directly. They must access it via getter and setter methods.

    Abstraction is used to hiding something too but in a higher degree(class, interface). Clients use an abstract class(or interface) do not care about who or which it was, they just need to know what it can do.

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