Difference between abstraction and encapsulation?

  • 1

    Difference between abstraction and encapsulation?

    Encapsulation is the packing of data and functions operating on that data into a single component and restricting the access to some of the object's components.
    Encapsulation means that the internal representation of an object is generally hidden from view outside of the object's definition.

    Abstraction is a mechanism which represent the essential features without including implementation details.

    Encapsulation:-- Information hiding.
    Abstraction:-- Implementation hiding.


    class foo{
            int a, b;
            foo(): a(0), b(0) 
            { } 
            foo(int x, int y): a(x), b(y) 
            { }
            int add()
                return a+b;   

    Internal representation of any object of foo class is hidden outside the class. --> Encapsulation.
    Any accessible member (data/function) of an object of foo is restricted and can only be accessed by that object only.

    foo foo_obj(3, 4);
    int sum = foo_obj.add();
    Implementation of method add is hidden. --> Abstraction.


  • 0

    Abstraction is to abstract the common feature of a class, no need to care about the detail.
    While Encapsulation is to pack the variable and method to a single object.

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.