Object Oriented Programming Using C#: Part 10

Before reading this article, please go through the following articles:

  1. Object Oriented Programming Using C#: Part 1
  2. Object Oriented Programming Using C#: Part 2
  3. Object Oriented Programming Using C#: Part 3
  4. Object Oriented Programming Using C#: Part 4
  5. Object Oriented Programming Using C#: Part 5
  6. Object Oriented Programming Using C#: Part 6
  7. Object Oriented Programming Using C#: Part 7
  8. Object Oriented Programming Using C#: Part 8
  9. Object Oriented Programming Using C#: Part 9

Difference between Encapsulation and Abstraction

There is a very basic difference between encapsulation and abstraction for beginners of OOP. They might get confused by it. But there is huge difference between them if you understand both the topics in detail.

Abstraction means to hide the unnecessary data from the user. The user only needs the required functionality or the output according to his requirements. For example a digital camera.

Dear reader, whenever we use a digital camera, we just click on the Zoom In and Zoom Out buttons and the camera zooms in and out but we can feel the lens moving. If we open the camera then we will see its complex mechanism that we can't understand. So pressing the button and getting the results according to your demand is the abstraction.

OOP1.jpg

OOP2.jpg

Encapsulation is simply combining the data members and functions into a single entity called an object.

If we consider the camera example again, when we press zoom In/Out buttons, inside the camera it uses mechanisms that consists of gears and lenses to zoom in or zoom out. The combination of gears and lenses is called encapsulation that will help the zooming functionality to work smoothly.

In simple words we can say that "Abstraction is achieved through encapsulation".

Or

Abstraction solves the problem in the design side while encapsulation is the implementation.

Example

OOP3.jpg
OOP4.jpg

Output

OOP5.jpg

Block A

  • In this session we defined an abstract class.
  • Having one protected variable and one function.

Block B

  • In this session we have defined a class with an abstract class as the base class.
  • With two public variables and two functions.
  • One property zoomresult that is overriden from the base class
  • In this block we have changed the value of the gear and lens in order to change the size of the _picturesize variable.

Block C

  • In this session we have created the object of the class camerazoom.
  • Then we have called the method zoom in
  • After that we have shown the output of the variable _picturesize using property.
  • Then we again call the method zoom in and show the results of the variable _picturesize .
  • In the final part we have called the method zoom out the picture and checked the result.


Similar Articles