- Abstraction is focused mainly on what should be done while Encapsulation is focused on how it should be done.
- Abstraction hides complexity by giving you a more abstract picture while Encapsulation hides internal working so that you can change it later.
- Abstraction solves problem at design level while Encapsulation solves problem at implementation level.
- Abstraction hides the irrelevant details found in the code whereas Encapsulation helps developers to organize the entire code easily.
Both Abstraction and Encapsulation are two of the four basic OOP concepts which allow you to model real-world things into objects so that you can implement them in your program and code. Many beginners get confused between Abstraction and Encapsulation because they both look very similar. If you ask someone what is Abstraction, he will tell that it's an OOP concept which focuses on relevant information by hiding unnecessary detail, and when you ask about Encapsulation, many will tell that it's another OOP concept which hides data from the outside world. The definitions are not wrong as both Abstraction and Encapsulation does hide something, but the key difference is on intent.
Abstraction hides complexity by giving you a more abstract picture, a sort of 10,000 feet view, while Encapsulation hides internal working so that you can change it later. In other words, Abstraction hides details at the design level, while Encapsulation hides details at the implementation level.
For example, when you first describe an object, you talk in more abstract term e.g. a Vehicle which can move, you don't tell how Vehicle will move, whether it will move by using tires or it will fly or it will sell. It just moves. This is called Abstraction. We are talking about the most essential thing, which is moving, rather than focusing on details like moving in-plane, sky, or water.
There are also different levels of Abstraction and it's good practice that classes should interact with other classes with the same level of abstraction or higher level of abstraction. As you increase the level of Abstraction, things start getting simpler and simpler because you leave out details.
On the other hand, Encapsulation is all about implementation. Its sole purpose is to hide the internal working of objects from the outside world so that you can change it later without impacting outside clients.
For example, we have a HashMap which allows you to store the object using the put() method and retrieve the object using the get() method. How HashMap implements this method (see here) is an internal detail of HashMap, the client only cares that put stores the object and get return it back, they are not concerned whether HashMap is using an array, how it is resolving the collision, whether it is using linked list or binary tree to store object landing on the same bucket, etc.
Because of Encapsulation, you can change the internal implementation of HashMap with ease without impacting clients who are using HashMap. For example, in Java 8, the java.util.HashMap changes its implementation to use a binary tree instead of LinkedList to store objects in the same bucket after a certain threshold.
No comments:
Post a Comment