Abstraction is a process of hiding the implementation details from the user, only the functionality will be provided to the user. In other words, the user will have the information on what the object does instead of how it does it. Through the process of abstraction, a programmer hides all but the relevant data about an object.

In Java, abstraction is achieved by interfaces and abstract classes. Sometimes while implementing a given structure of a superclass we do not want to share the complete implementation of every method, which means the superclass only provides the generalized form to the subclasses leaving it for the subclasses to fill the details.

For example, a given superclass shape, which can be of different type, can have different properties, color, size and so on. There can be many types of shapes like circle, triangle, square and many more, each having its own properties. The hierarchy type can classify both similarity and differences differently.

// Java program to illustrate the concept of Abstraction 
abstract class Shape  
{ 
    String color; 
      
    // Abstract methods 
    abstract double area(); 
    public abstract String toString(); 
      
    // abstract class can have constructor 
    public Shape(String color) { 
        this.color = color; 
    } 
      
    // Concrete method 
    public String getColor() { 
        return color; 
    } 
} 

class Circle extends Shape 
{ 
    double radius; 
      
    public Circle(String color,double radius) { 
  
        // Calling Shape constructor 
        super(color); 
        this.radius = radius; 
    } 
  
    @Override
    double area() { 
        return Math.PI * Math.pow(radius, 2); 
    }       
} 

class Rectangle extends Shape{ 
  
    double length; 
    double width; 
      
    public Rectangle(String color,double length,double width) { 
        // Calling Shape constructor 
        super(color); 

        this.length = length; 
        this.width = width; 
    } 
      
    @Override
    double area() { 
        return length*width; 
    }   
} 

public class Test  
{ 
    public static void main(String[] args) 
    { 
        Shape s1 = new Circle("Red", 2.2); 
        Shape s2 = new Rectangle("Yellow", 2, 4); 
    } 
}

Example of abstraction: Lets say we have a mobile app for getting the latest stock quote. For the user its as simple as entering the company name or company stock code in the app and pressing the ‘fetch’ button. Internally the app would execute a sequence of steps like hooking on to the data connection/wifi, then invoking a RESTful API on a backend server. This backend server will hit the database or make another call to an external stock quote service provider to get the stock quote. The actual stock quote then travel backwards in the chain and gets displayed in the app.


Difference between Encapsulation and Abstraction

Abstraction is used to hide certain details and only show the essential features of the object. In other words, it deals with the outside view of an object (interface). Encapsulation is a language construct which bundles data and behavior together. Its basically about hiding the state of object with the help of modifiers like private, public, protected etc.

Every function is an encapsulation; in pseudocode:

point x = { 1, 4 }
point y = { 23, 42 }

numeric d = distance(x, y)

Here, distance encapsulates the calculation of the (Euclidean) distance between two points in a plane: it hides implementation details. This is an encapsulation. Abstraction is the process of generalisation: taking a concrete implementation and making it applicable to different, albeit somewhat related, types of data. The classical example of abstraction is C’s qsort function to sort data. qsort doesn’t care about the data it sorts — in fact, it doesn’t know what data it sorts. Its input type is a typeless pointer (void*). The important point is that the implementation of qsort always stays the same, regardless of data type. The only thing that has to change is the compare function, which differs from data type to data type.

Encapsulation is data hiding (information hiding) while Abstraction is detail hiding(implementation hiding).