Abstraction vs Encapsulation in java

            Encapsulation                Abstraction
1. Encapsulation is a concept for wrapping of data and code into a single unit.
2. Encapsulation is a way of data hiding.
3. Encapsulation is achieved by access modifiers and classes.
4. Encapsulation solve the problem at implementation level.
1. Abstraction is a way to show only essential details to user.
2. Abstraction is way of hiding complexity.
3. Abstraction is achieved by abstract class and interface.
4. Abstraction solve the problem at implementation level.

Encapsulation

Encapsulation is outlined as the wrapping up of data in a single unit. It’s a mechanism which binds the code and data. One other approach to consider encapsulation is a way of protecting data from being accessed by the code outside the unit.
Encapsulation Example

class Employee { 
    private String name; 
    private int id; 
    private int age; 
 
    public int getAge() 
    { 
        return age; 
    } 
 
    public void setAge(int age) 
    { 
        this.age = age; 
    }
 
    public String getName() 
    { 
        return name; 
    }
 
    public void setName(String name) 
    { 
        this.name = name; 
    } 
 
    public int getId() 
    { 
        return id; 
    } 
 
    public void setId(int id) 
    { 
        this.id = id; 
    } 
} 
 
public class Main { 
    public static void main(String[] args) 
    { 
        Employee obj = new Employee(); 
 
        obj.setName("Jai"); 
        obj.setAge(29); 
        obj.setId(10); 
 
        System.out.println("Employee's name: " + obj.getName()); 
        System.out.println("Employee's age: " + obj.getAge()); 
        System.out.println("Employee's id: " + obj.getId()); 
    } 
}

Output

Employee's name: Jai
Employee's age: 29
Employee's id: 10

Compiler will throw error, if we try to access data members directly.
Encapsulation Example

class Employee { 
    private String name; 
    private int id; 
    private int age; 
 
    public int getAge() 
    { 
        return age; 
    } 
 
    public void setAge(int age) 
    { 
        this.age = age; 
    }
 
    public String getName() 
    { 
        return name; 
    }
 
    public void setName(String name) 
    { 
        this.name = name; 
    } 
 
    public int getId() 
    { 
        return id; 
    } 
 
    public void setId(int id) 
    { 
        this.id = id; 
    } 
} 
 
public class Main { 
    public static void main(String[] args) 
    { 
        Employee obj = new Employee(); 
 
        obj.setName("Jai"); 
        obj.setAge(29); 
        obj.setId(10); 
 
        System.out.println("Employee's name: " + obj.name); 
        System.out.println("Employee's age: " + obj.age); 
        System.out.println("Employee's id: " + obj.id); 
    } 
}

Output

Main.java:46: error: name has private access in Employee
        System.out.println("Employee's name: " + obj.name); 
                                                    ^
Main.java:47: error: age has private access in Employee
        System.out.println("Employee's age: " + obj.age); 
                                                   ^
Main.java:48: error: id has private access in Employee
        System.out.println("Employee's id: " + obj.id); 
                                                  ^
3 errors

Abstraction

Data Abstraction is a way of hiding complexity not data or we can say that it is a mechanism which only show the important details to the person or user. The non-essentials or trivial details are usually not exhibited to the user or person. For example: When a user sends a message from whatsapp, He/She only need to type the message and click on send button, the details  like how the message will be sent internally, will be hidden from user.
Abstraction Example

abstract class GraphicTest{
                abstract void displayShape();
}
 
class Circle extends GraphicTest{
                void displayShape() {
                      System.out.println("Graphic Object type is Circle.");
                }     
}
 
class Rectangle extends GraphicTest{
                void displayShape() {
                     System.out.println("Graphic Object type is Rectangle.");
               }  
}
 
class Triangle extends GraphicTest{
                void displayShape() {
                     System.out.println("Graphic Object type is Triangle.");
                }             
 
}
 
public class Main {
                public static void main(String args[]){
                       GraphicTest obj = new Circle();
                       obj.displayShape();
                       obj = new Rectangle();
                       obj.displayShape();
                       obj = new Triangle();
                       obj.displayShape();
                }  
}

Output

Graphic Object type is Circle. 
Graphic Object type is Rectangle.
Graphic Object type is Triangle.