What's the difference between abstraction and encapsulation?

前端 未结 24 1018
攒了一身酷
攒了一身酷 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:47

    Abstraction : Abstraction is process in which you collect or gather relevant data and remove non-relevant data. (And if you have achieved abstraction, then encapsulation also achieved.)

    Encapsulation: Encapsulation is a process in which you wrap of functions and members in a single unit. Means You are hiding the implementation detail. Means user can access by making object of class, he/she can't see detail.

    Example:

     public class Test
       {
        int t;
        string  s;
     public void show()
      {
       s = "Testing";
       Console.WriteLine(s);
       Console.WriteLine(See()); // No error
      }
    
     int See()
      {
       t = 10;
       return t;
      }
    
     public static void Main()
      {
      Test obj =  new Test();
      obj.Show(); // there is no error
      obj.See(); // Error:- Inaccessible due to its protection level
      }
     }
    

    In the above example, User can access only Show() method by using obj, that is Abstraction.

    And See() method is calling internally in Show() method that is encapsulation, because user doesn't know what things are going on in Show() method.

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

    ABSTRACTION:"A view of a problem that extracts the essential information relevant to a particular purpose and ignores the remainder of the information."[IEEE, 1983]

    ENCAPSULATION: "Encapsulation or equivalently information hiding refers to the practice of including within an object everything it needs, and furthermore doing this in such a way that no other object need ever be aware of this internal structure."

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

    As far as iOS is concerned, it can be said that Objective C files (i.e. .h and .m) use abstraction as well as encapsulation.

    Abstraction

    Header file (.h) only exposes the functions and public members to outside world. No one knows how they are used unless they have the implementation file with them. It is the .m file that holds all the usage and implementation logic with it self. "Implementation remains unexposed".

    Encapsulation

    The property (@property) encapsulates the memory management attribute (atomic, strong, retain, weak) of an iVar.

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

    Abstraction has to do with separating interface from implementation. (We don't care what it is, we care that it works a certain way.)

    Encapsulation has to do with disallowing access to or knowledge of internal structures of an implementation. (We don't care or need to see how it works, only that it does.)

    Some people do use encapsulation as a synonym for abstraction, which is (IMO) incorrect. It's possible that your interviewer thought this. If that is the case then you were each talking about two different things when you referred to "encapsulation."


    It's worth noting that these concepts are represented differently in different programming languages. A few examples:

    • In Java and C#, interfaces (and, to some degree, abstract classes) provide abstraction, while access modifiers provide encapsulation.
    • It's mostly the same deal in C++, except that we don't have interfaces, we only have abstract classes.
    • In JavaScript, duck typing provides abstraction, and closure provides encapsulation. (Naming convention can also provide encapsulation, but this only works if all parties agree to follow it.)
    0 讨论(0)
  • 2020-12-22 17:54

    Abstraction is one of the many benefits of Data Encapsulation. We can also say Data Encapsulation is one way to implement Abstraction.

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

    Abstraction

    Exposing the Entity instead of the details of the entity.

    "Details are there, but we do not consider them. They are not required."

    Example 1:

    Various calculations: Addition, Multiplication, Subtraction, Division, Square, Sin, Cos, Tan.

    We do not show the details of how do we calculate the Sin, Cos or Tan. We just Show Calculator and it's various Methods which will be, and which needs to be used by the user.

    Example 2:

    Employee has: First Name, Last Name, Middle Name. He can Login(), Logout(), DoWork().

    Many processes might be happening for Logging employee In, such as connecting to database, sending Employee ID and Password, receiving reply from Database. Although above details are present, we will hide the details and expose only "Employee".

    Encapsulation

    Enclosing. Treating multiple characteristics/ functions as one unit instead of individuals. So that outside world will refer to that unit instead of it's details directly.

    "Details are there, we consider them, but do not show them, instead we show what you need to see."

    Example 1:

    Instead of calling it as Addition, Subtraction, Multiplication, Division, Now we will call it as a Calculator.

    Example 2:

    All characteristics and operations are now referred by the employee, such as "John". John Has name. John Can DoWork(). John can Login().

    Hiding

    Hiding the implemention from outside world. So that outside world will not see what should not be seen.

    "Details are there, we consider them, but we do not show them. You do not need to see them."

    Example 1:

    Your requirement: Addition, Substraction, Multiplication, Division. You will be able to see it and get the result.

    You do not need to know where operands are getting stored. Its not your requirement.

    Also, every instruction that I am executing, is also not your requirement.

    Example 2:

    John Would like to know his percentage of attendance. So GetAttendancePercentage() Will be called.

    However, this method needs data saved in database. Hence it will call FetchDataFromDB(). FetchDataFromDB() is NOT required to be visible to outside world.

    Hence we will hide it. However, John.GetAttendancePercentage() will be visible to outside world.

    Abstraction, encapsulation and hiding complement each others.

    Because we create level of abstraction over details, the details are encapsulated. And because they are enclosed, they are hidden.

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