Sunday, September 8, 2013

What is Encapsulation ? and Abstraction vs Encapsulation

  • It’s wrapping of data members and functions into a single unit called as class.
  • An encapsulated object is often called an abstract data type.
  • Advantage: Encapsulation can protect your data from accidental corruption.

Example:
Let's take example of substring() function which returns the string based on the start index and length. The underlying algorithm and the internal data structures used to find the substring is specific to the String class and hidden from the external code who makes use of Substring() in which case the external code does not have privilege’s to modify the algorithm or the data structures which are internal to (or encapsulated in) String class. This is known as encapsulation.













Difference between abstraction and encapsulation

Abstraction Encapsulation
Abstraction is used for hiding the unwanted data and giving relevant data. Encapsulation means hiding the code and data in to a single unit to protect the data from outside world.
Abstraction solves the problem in the design level Encapsulation solves the problem in the implementation level

No comments:

Post a Comment