Abstraction & Encapsulation in Java

Abstraction

Abstraction is the technique of hiding the implementation details and showing only functionality to the user.
For example, when we call any person we just dial the numbers and are least bothered about the internal process involved.

Abstract class

  • A class that can contain abstract keyword is known as an abstract class.
  • Abstract methods do not have body.
  • It forces the user to extend the implementation rather than modification.
  • An abstract class cannot be instantiated.

Abstract Method

  • If a method is declared as an abstract, then it is called abstract method.
  • It does not have implementation.
  • It contains method signature but not method body.
Syntax:

abstract class Student
{
    private String name;
    private int Rollno
    private String course;
    public abstract int m1()
    {
    }
}


Example: Program for abstract class and method

abstract class Vehicle
{    
    abstract int getSpeed();    
}    
class Bike extends Vehicle
{    
    int getSpeed()
   {
        return 60;
   }    
}    
class Car extends Vehicle
{    
     int getSpeed()
     {
        return 70;
     }    
}    
public class TestVehicle
{    
    public static void main(String args[])
   {    
       Vehicle bike = new Bike();
       Vehicle car = new Car();
       int bikespeed = bike.getSpeed();    
       int carspeed = car.getSpeed();
       System.out.println("Speed of Bike is: "+bikespeed+" Km/h");
       System.out.println("Speed of Car is: "+carspeed+" Km/h");
    }
}


Output:
Speed of Bike is: 60 Km/h
Speed of Car is: 70 Km/h

Difference between Abstract Class and Final Class

Abstract ClassFinal Class
Inheritance can be allowed.Inheritance cannot be allowed.
It contains abstract method.It cannot contain abstract method.
Abstract method must be overridden.Final method cannot be overridden.

Encapsulation


  • Encapsulation is the mechanism to bind together code (methods) and data (variables) into a single unit.
  • Encapsulation is used for information hiding.
  • Encapsulation can be achieved by declaring variables of class as private.

Advantages of Encapsulation

  • Using getter and setter method, the field of the class can be made read-only or write-only.
  • It improves the flexibility & maintainability of the code.
  • By encapsulation we can hide the implementation details.
  • Class has total control over the data.

Example: Program to use setter getter method in Java.

// EmpSetGet.java

public class EmpSetGet
{
   private int empId;
   private String name;
   private int age;
   private String dept;

   public int getEmpId()
   {
      return empId;
   }
   public String getName()
   {
      return name;
   }
   public int getAge()
   {
      return age;
   }
   public String getDept()
   {
       return dept;
   }
   public void setEmpId( int empId)
   {
      this.empId = empId;
   }
   public void setName(String name)
   {
      this.name = name;
   }
   public void setAge( int age)
   {
      this.age = age;
   }
   public void setDept(String dept)
   {
       this.dept = dept;
   }
}


Example:

//Employee.java

public class Employee
{
    public static void main(String args[])
    {
      EmpSetGet emp = new EmpSetGet();
      emp.setEmpId(101);
      emp.setName("ABC");
      emp.setAge(25);
      emp.setDept("Testing");
      System.out.print("Employee Id : "+emp.getEmpId() + "\nName : " + emp.getName() + "\nAge : " + emp.getAge() + "\nDepartment : "+emp.getDept());
   }
}


Output:
Employee Id : 101
Name : ABC
Age : 25
Department : Testing