Difference between abstraction and encapsulation?


  • 1
    S

    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.


    Example:

    class foo{
        private:
            int a, b;
        public:
            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.

    Ref:
    https://stackoverflow.com/questions/742341/difference-between-abstraction-and-encapsulation?page=1&tab=active#tab-top


Log in to reply
 

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