Monday, November 16, 2015

What is Abstraction ?


Abstraction is hiding the implementation from the user. It will only focus on the essentials and it will ignore the irrelevant. It will shows only the functionality. Java Abstraction is achieved using Abstract classes, and Interfaces.

If we take an example of a chair we will just mention it as a chair we won’t say it is made of wood or glass and how it was made like wise. We will hide the irrelevant parts.

Abstraction Example:


If we take an example of an employee when we write all the properties of the employee it will look like this.

public  class Employee
{
   private String name;
   private String address;
   private String gender;
  
   private int number;
   private int salary;
   private int allowance;
  
   void getDetails(){
          //details methods
   }
  
    double computePay() {
       //compute the salary
              return 0;
       }
  
 
}

But we can see that rather than having an employee class we can group details and the salary to another group. So it will look like this.


public class Salary {
  private int number;
   private int salary;
   private int allowance;
  
double computePay() {
       //compute the salary
              return 0;
       }
     
}


public class Details {
  private String name;
  private String address;
  private String gender;

     void getDetails(){
          //details methods
   }  
}

So you can see the attributes and the methods realated to the Details and Salary are moved.

From having java abstraction we can  reduce the complexity and also it will improve the maintainability of the system.

No comments:

Post a Comment