Encapsulation In Java With Real Time Example With Diagram

Encapsulation is one of the most basic fundamental concepts of OOP’s.  As name suggests, it encapsulate the data and methods together inside class. Some more points are listed below,

Concepts of Java, Encapsulation, Encapsulation with diagram, Encapsulation with real time example, Java, java concept, OOPs concept, OOPs concept of java
Encapsulation
  • The process of wrapping up code and data together into a single unit is called Encapsulation.
  • Due to encapsulation, data will be safe from the outside world by binding the data and codes.
  • Through Public method, we get controlled access to its data.
  • Java provides 4 different access modifiers to control visibility of internal data from outside world. private, protected, default (no access modifier) and public.
  • Purpose of Hiding the data is to give protection.

Important points on Encapsulation

1. What is Encapsulation?

  • It is the mechanism of making the field inside the class private and providing access to these fields by public methods thereby hiding the fields within the class. So it is called Data Hiding.

2. How can we achieve encapsulation?

  • By declaring class variables as private.
  • Public getter and setter methods to set and access those variables.

Example1

Bank gives controlled access to check our balance, amount deposit or amount withdraw by providing methods like balanceCheck(), amtDeposit(), amtWithdraw() etc.

3. What are the primary benefits of encapsulation?

  • It improves maintainability and flexibility and re-usability.
  • Encapsulation helps to make our code secure.
  • Control the way data is accessed or modified.
  • Encapsulation in Java makes unit testing easy.
  • Encapsulation allows you to change one part of code without affecting other parts of code.

4. What if encapsulation not implemented?

If proper encapsulation is not followed in a code, it leads to its bad designing.

In below example, we considered employee information by setting employee name, age and ssn number as private. And this information we get controlled access by public methods.

Example2

public class encapsulation{
    public static void main(String args[]){
   	//create object
         EncapsulationDemo obj = new EncapsulationDemo();
         obj.setEmpName("Mario");
         obj.setEmpAge(32);
         obj.setEmpSSN(112233);
         System.out.println("Employee Name: " + obj.getEmpName());
         System.out.println("Employee SSN: " + obj.getEmpSSN());
         System.out.println("Employee Age: " + obj.getEmpAge());
    } 
}
class EncapsulationDemo{
    private int ssn;
    private String empName;
    private int empAge;

    //Getter and Setter methods
    public int getEmpSSN(){
        return ssn;
    }

    public String getEmpName(){
        return empName;
    }

    public int getEmpAge(){
        return empAge;
    }

    public void setEmpAge(int newValue){
        empAge = newValue;
    }

    public void setEmpName(String newValue){
        empName = newValue;
    }

    public void setEmpSSN(int newValue){
        ssn = newValue;
    }
}

/*
 * Output:
	Employee Name: 	Mario
	Employee SSN: 	112233
	Employee Age: 	32 
 * 
*/

Recommended post in OOPs concept:

Leave a Comment

Your email address will not be published.