Abstraction

In java, the Abstraction is hiding the complexity of workflow from the end user.
abstraction is implemented with the help of the class, objects, and variables that can represent complex underlying code and data. Abstraction eliminated the need for new code creation every time and increases code reusability. Abstraction is a process of hiding the implementation details from the user. Оnly the functionality will be provided to the user. In Java, abstraction is achieved using abstract classes and interfaces. 

In order to implement abstraction, Java programmers create reusable and useful tools. Like several objects that may have variables, functions, and data structures can be created. Different classes to create the objects can also be created by the programmers.

Abstraction is one of the four major concepts behind object-oriented programming (OOP). OOP questions are very common on job interviews, so you may expect questions about abstraction on your next Java job interview.

Java Abstraction Example

To give an example of abstraction we will create one superclass called Employee and two subclasses –  Contractor and FullTimeEmployee. Both subclasses have common properties to share, like the name of the employee and the the amount of money the person will be paid per hour. There is one major difference between contractors and full-time employees – the tyme they work for the company. Full-time employees work constantly 8 hours per day and the working time of contractors may vary.
Java abstract class example
Java abstract class example
Lets first create the superclass Employee. Note the usage of abstract keyword in class definition. This marks the class to be abstract, which means it can not be instantiated directly. We define a method called calculateSalary() as an abstract method. This way you leave the implementation of this method to the inheritors of the Employee class.

public abstract class Employee {
 
 private String name;
 private int paymentPerHour;
 
 public Employee(String name, int paymentPerHour) {
  this.name = name;
  this.paymentPerHour = paymentPerHour;
 }
 
 public abstract int calculateSalary();

 public String getName() {
  return name;
 }

 public void setName(String name) {
  this.name = name;
 }

 public int getPaymentPerHour() {
  return paymentPerHour;
 }

 public void setPaymentPerHour(int paymentPerHour) {
  this.paymentPerHour = paymentPerHour;
 }

}
 
The Contractor class inherits all properties from its parent Employee but have to 
provide it’s own implementation to calculateSalary() method. In this case we 
multiply the value of payment per hour with given working hours. 

public class Contractor extends Employee {
 
 private int workingHours;

 public Contractor(String name, int paymentPerHour, int workingHours) {
  super(name, paymentPerHour);
  this.workingHours = workingHours;
 }

 @Override
 public int calculateSalary() {
  return getPaymentPerHour() * workingHours;
 }

}
 
The FullTimeEmployee also has it’s own implementation ofcalculateSalary()method. In this case we just multiply by constant 8 hours.

public class FullTimeEmployee extends Employee {

 public FullTimeEmployee(String name, int paymentPerHour) {
  super(name, paymentPerHour);
 }

 @Override
 public int calculateSalary() {
  return getPaymentPerHour() * 8;
 }

}