What is Encapsulation? 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
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.
Hope this clears you concept of abstraction.
Abstraction Vs 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.