locked
Encapsulation, abstraction RRS feed

  • Question

  • I googled encapsulation
    some where i read that a class is an example of encapsulation because it binds data n methods in a unit.

    And some where i read it hides the information of method by access modifiers.

    And I got more confused when i read the hiding of information is abstraction.

    Kindly give me example of encapsulation & abstraction by programming and by real world.


    Thursday, April 5, 2012 9:21 AM

Answers

  • Hello,

    In my mind, Information Hiding is the principle whereas encapsulation is the impleementation of the principle in OOP languages and the outcome is an abstraction between the consumer and the dealer whereas the syntax of the language is the tool to achieve that goal.


    Eyal (http://shilony.net), Regards.



    • Edited by Eyal Solnik Thursday, April 5, 2012 10:58 AM
    • Proposed as answer by Stefan Hoffmann Thursday, April 5, 2012 11:02 AM
    • Marked as answer by Bob Shen Wednesday, April 18, 2012 5:54 AM
    Thursday, April 5, 2012 10:51 AM
  • Take a look at Encapsulation: It means that the internals of the object are "hidden" from the objects consumer. Using only the visible methods means that you use an abstraction as you don't need to know anything about the internals to use it.

    • Marked as answer by Bob Shen Wednesday, April 18, 2012 5:54 AM
    Thursday, April 5, 2012 9:56 AM
  • Abstraction:
    Abstraction is only giving essential details w/o any background information. It can be achieved by abstract class.

    You don't need abstract classes to have abstraction in code.  Almost every class is an abstraction of some sort.  

    I know that if I want to calculate the square root of a number I can use Math.Sqrt().  I don't need to know the algorithm it uses, I just need to know that it works, what the valid inputs are, and what outputs I will get based on that input (in this case, the square root of the number, and also some edge cases with NAN, infinity, etc.).

    When I create a new object I don't need to know that there is a heap, and that the heap pointer will ensure that there is enough space for the object, move the heap pointer up, clear out the space, and so on and so on.  I don't need to know where in virtual or physical memory it's located, or even how many bytes of data it consumes.  I just know that if I put "new MyClass();" I'll get back a new instance of the class and that the language/windows/hardware/other will work together to take care of the rest.  This is because of abstraction.  Layers built upon layers in which each handles some aspect of functionality, using one layer, and exposing new functionality to the layer(s) above it.

    • Marked as answer by Bob Shen Wednesday, April 18, 2012 5:54 AM
    Thursday, April 5, 2012 2:38 PM
  • Concept of Abstraction and Encapsulation are complementary. 

    Abstraction is basically externalize the essential features of an object, it provides a simplified view of an object. 

    An example from .NET framework, HttpWebRequest and FtpWebRequest. First one make web request over Http and other make the request over Ftp. So concept of making a request(WebRequest) on URL has been abstracted in both of the class. Same as Stream is abstract concept. Any other stream, like FileStream, NetworkStream, BufferStream - you can easily say the Streaming concept(representing sequence of bytes) has been abstracted into those streams.

    See, before writing any thesis, you write the abstract, the essential and simplified view of the rest of the descriptions of the papers. 

    Encapsulation is hiding the internal details into the object itself. Do not confuse the work "Hiding" to any security purpose, rather think it more like exposing purpose. You should decide what should be exposed from the object or hide in the object. Remember in our old procedure programming world, there was not any clean separations of attributes and behavior of any entity. OOPs concept first encapsulate the attributes and behavior into an object and have controls over what needs to be exposed and what not.

    So bottom line, Abstraction provides essential and simplified view of an object and Encapsulation is hiding the internal details into an object.

    • Marked as answer by Bob Shen Wednesday, April 18, 2012 5:54 AM
    Thursday, April 5, 2012 4:17 PM

All replies

  • Take a look at Encapsulation: It means that the internals of the object are "hidden" from the objects consumer. Using only the visible methods means that you use an abstraction as you don't need to know anything about the internals to use it.

    • Marked as answer by Bob Shen Wednesday, April 18, 2012 5:54 AM
    Thursday, April 5, 2012 9:56 AM
  • Hello,

    In my mind, Information Hiding is the principle whereas encapsulation is the impleementation of the principle in OOP languages and the outcome is an abstraction between the consumer and the dealer whereas the syntax of the language is the tool to achieve that goal.


    Eyal (http://shilony.net), Regards.



    • Edited by Eyal Solnik Thursday, April 5, 2012 10:58 AM
    • Proposed as answer by Stefan Hoffmann Thursday, April 5, 2012 11:02 AM
    • Marked as answer by Bob Shen Wednesday, April 18, 2012 5:54 AM
    Thursday, April 5, 2012 10:51 AM
  • HI,

    Abstraction:
    Abstraction is only giving essential details w/o any background information. It can be achieved by abstract class.

    Real world example is car.
    we can see the accelerator, brake etc. we just press it but dont see the internal functionalities.

    Encapsulation: Hiding of data, member functions etc. eg class.

    real world example: capsule tablet, bonda(hides masala, carrot, onion into flour)
    Thursday, April 5, 2012 1:39 PM
  • Abstraction:
    Abstraction is only giving essential details w/o any background information. It can be achieved by abstract class.

    You don't need abstract classes to have abstraction in code.  Almost every class is an abstraction of some sort.  

    I know that if I want to calculate the square root of a number I can use Math.Sqrt().  I don't need to know the algorithm it uses, I just need to know that it works, what the valid inputs are, and what outputs I will get based on that input (in this case, the square root of the number, and also some edge cases with NAN, infinity, etc.).

    When I create a new object I don't need to know that there is a heap, and that the heap pointer will ensure that there is enough space for the object, move the heap pointer up, clear out the space, and so on and so on.  I don't need to know where in virtual or physical memory it's located, or even how many bytes of data it consumes.  I just know that if I put "new MyClass();" I'll get back a new instance of the class and that the language/windows/hardware/other will work together to take care of the rest.  This is because of abstraction.  Layers built upon layers in which each handles some aspect of functionality, using one layer, and exposing new functionality to the layer(s) above it.

    • Marked as answer by Bob Shen Wednesday, April 18, 2012 5:54 AM
    Thursday, April 5, 2012 2:38 PM
  • Concept of Abstraction and Encapsulation are complementary. 

    Abstraction is basically externalize the essential features of an object, it provides a simplified view of an object. 

    An example from .NET framework, HttpWebRequest and FtpWebRequest. First one make web request over Http and other make the request over Ftp. So concept of making a request(WebRequest) on URL has been abstracted in both of the class. Same as Stream is abstract concept. Any other stream, like FileStream, NetworkStream, BufferStream - you can easily say the Streaming concept(representing sequence of bytes) has been abstracted into those streams.

    See, before writing any thesis, you write the abstract, the essential and simplified view of the rest of the descriptions of the papers. 

    Encapsulation is hiding the internal details into the object itself. Do not confuse the work "Hiding" to any security purpose, rather think it more like exposing purpose. You should decide what should be exposed from the object or hide in the object. Remember in our old procedure programming world, there was not any clean separations of attributes and behavior of any entity. OOPs concept first encapsulate the attributes and behavior into an object and have controls over what needs to be exposed and what not.

    So bottom line, Abstraction provides essential and simplified view of an object and Encapsulation is hiding the internal details into an object.

    • Marked as answer by Bob Shen Wednesday, April 18, 2012 5:54 AM
    Thursday, April 5, 2012 4:17 PM