locked
DIFFERENCE BETWEEN ABSTRACTION AND ENCAPSULATION WITH EXAMPLES IN C# ?? RRS feed

  • Question

  • can anyone please tell me the basic difference between abstraction and encapsulation with example.

    I have done lot of searches on the internet but didn't get anything which would prove the difference .

    so,it would be helpful if anyone guide me through the basic difference on the subject (with codes as the example).

    Saturday, May 3, 2014 12:56 PM

Answers

  • in addition to the above excellent reply:

    • encapsulation helps you to bring together an object's properties & functions but abstraction means to simply "abstract" or hide away complexity.
    • by encapsulation you do achieve abstraction as well because you do end up implementing functions / behaviors of the object & so you do end up achieving abstraction as well right?
    • you can use classes, etc to achieve encapsulation. abstraction, well there are numerous ways (apart from using classes: interfaces, virtual, abstract, etc)

    eg of encapsulation:

    //bringing together object's properties & methods / functionality together
    class SomeClass
    {
     //public properties
     //public methods
    }

    focused eg of abstraction:

    interface IMyContract
    {
     // interface members
    }

    hope this helps!


    Saturday, May 3, 2014 2:01 PM

All replies

  • A priori, they've got nothing in common.

    point x = { 1, 4 };
    point y = { 23, 42 };
    
    int d = distance(x, y);
    

    Here, distance encapsulates the calculation of the (euclidean) distance between two points in a plane: it hides implementation details. This is encapsulation, pure and simple.

    Abstraction is the process of generalization: taking a concrete implementation and making it applicable to different, albeit somewhat related, types of data. The classical example of abstraction is C's qsortfunction which sorts data.

    The thing about qsort is that it doesn't care about the data it sorts – in fact, it doesn't know what data it sorts. Rather, its input type is a typeless pointer (void*) which is just C's way of saying “I don't care about the type of data” (this is also called type erasure). The important point is that the implementation of qsort always stays the same, regardless of data type. The only thing that has to change is the compare function, which differs from data type to data type. qsort therefore expects the user to provide said compare function as a function argument.




    Saturday, May 3, 2014 1:21 PM
  • in addition to the above excellent reply:

    • encapsulation helps you to bring together an object's properties & functions but abstraction means to simply "abstract" or hide away complexity.
    • by encapsulation you do achieve abstraction as well because you do end up implementing functions / behaviors of the object & so you do end up achieving abstraction as well right?
    • you can use classes, etc to achieve encapsulation. abstraction, well there are numerous ways (apart from using classes: interfaces, virtual, abstract, etc)

    eg of encapsulation:

    //bringing together object's properties & methods / functionality together
    class SomeClass
    {
     //public properties
     //public methods
    }

    focused eg of abstraction:

    interface IMyContract
    {
     // interface members
    }

    hope this helps!


    Saturday, May 3, 2014 2:01 PM