Abstraction is a process to hide the implementation details from the users and showing only the functionality. It displays only essential things.
For example: Like ATM Machine, we can see only functionality but don’t know internal details that how it works.
• Increases reusability of code by minimizes code redundancy.
• Separates program into code and implementation.
• Increases code readability.
In java abstraction is achieved in two ways-
• By using abstract class
Abstract class is declared with abstract keyword– it can contain both abstract and non-abstract methods.
Abstract methods are methods that can contain methods without body i.e. undefined methods.
Non-abstract methods are methods that contain methods with body i.e. defined methods.
Abstract class is used to achieve abstraction.
Abstract class cannot be instantiated but can be sub classed.
A simple program to show how abstraction class works in java.
It is mechanism to encapsulate or wrap up all the data into a single entity it is known as encapsulation.
Encapsulation can be achieved in two ways:
1. By using setter and getter methods.
2. Making all variables as private.
For Example: Here ATM Machine is a class. It contains both data i.e. money and operations i.e. withdraw money, balance enquiry and they are integrated in a single entity called ATM. This is called Encapsulation.