Abstraction in the real world:
The abstraction is a way of hiding complexity. Let us take the example of a car. We know that if the accelerator is pressed, speed will increase but don’t know the internal process of how speed will be increased.
Abstraction in programming:
The abstraction is a way to show essential details (what) to the user and hide non-essential details (how). Let us take a simple example:
result= num1*num2;
In the above example, we multiply two numbers and store the result in a new variable. But what is happening behind the scenes? There are so many things like registers, the program counter (PC), etc. are involved. A number of operations like PUSH, POP, etc. are happening. But these operations are hidden by high-level languages we are using for programming.
Abstraction in Java is achieved with the help of abstract classes and interfaces. We will discuss later about abstract class and interface later.
Advantages/Benefits of Abstraction:
- Hide complexity.
- Only show essential details to the end user.
Difference between abstraction and encapsulation:
Encapsulation | Abstraction |
1. Encapsulation is a concept for wrapping data and code into a single unit. 2. Encapsulation is a way of data hiding. 3. Encapsulation can be implemented by access modifiers and classes. |
1. Abstraction is a way to show only essential details to the user. 2. Abstraction is a way of hiding complexity. 3. Abstraction can be implemented by abstract class and interface. |