Difference between Abstraction and Encapsulation in Java
Definition of Abstraction
Abstraction is an OOP concept that focuses only on relevant data of an object. It hides the background details and emphasizes the essential data points for reducing the complexity and increase efficiency. It generally retains only information which is most relevant for that specific process. Abstraction method mainly focusses on the idea instead of actual functioning.
Definition of Encapsulation
Encapsulation is a method of making a complex system easier to handle for end users. The user need not worry about internal details and complexities of the system. Encapsulation is a process of wrapping the data and the code, that operate on the data into a single entity. You can assume it as a protective wrapper that stops random access of code defined outside that wrapper.
Abstraction vs Encapsulation
Difference between Abstraction and Encapsulation
Here is the main difference between Abstraction vs Encapsulation:
Why you need Abstraction?
Here, are the main reasons why abstraction is needed for Object-Oriented Programming:
Helps you to simplify the representation of the domain models. Abstraction hides the irrelevant details found in the code. Abstraction helps you to partition the program into many independent concepts. Offers the greatest flexibility when using ADT(Abstract Data Type) objects in different situations
Why you need Encapsulation?
The main advantages of encapsulation are
It improves the maintainability of an application. Offers flexibility to the user to use the system very easily Helps the developers to organize the code better Makes the overall coding process easier, as you are only concerned with what another class does, not how it does it This method helps the developers to be more ‘objective’ and result oriented. Encapsulated Code is quite flexible and easy to change with new requirements. Encapsulation makes unit testing easy. It allows you to reduce coupling of modules and increases cohesion inside a module as all piece of one thing are encapsulated in one place. Encapsulation helps you to change a part of code without affecting other parts of the code. Improves the code readability of the Application Enhanced security and makes maintenance of application easier The encapsulation interface only allows for well-defined interaction.
Example of Abstraction
Suppose you want to create a banking application and you are asked to collect all the information about your customer. There are chances that you will come up with the following information about the customer.
Example of Abstraction in OOP But, not all of the above information is required to create a banking application. So, you need to select only the useful information for your banking application from that pool. Data like name, address, tax information, etc. make sense for a banking application.
Example of Abstraction in OOP Since we have fetched/removed/selected the customer information from a larger pool, the process is referred to as Abstraction. However, the same information once extracted can be used for a wide range of applications. For instance, you can use the same data for hospital application, job portal application, a Government database, etc. with little or no modification. Hence, it becomes your Master Data. This is an advantage of Abstraction.
Example of Encapsulation
Let’s take an example of mobile device. With the help of mobile devices, you can perform various functions like taking a picture, sending a message, recording video/ audio, access the web and much more. The features mentioned above are functionalities of most of the smartphone. However, you don’t need to understand the internal functioning details of those features before using this program. Example, you don’t need to know how your camera calculates gamma correction or identifies a human face in an image. You just need to learn the software interface. This is encapsulation.