dsa

Java Encapsulation

Introduction :

In this tutorial, you will learn about encapsulation and data hiding in Java with the help of examples.

What is Encapsulation ?

Encapsulation simply means binding object state(fields) and behaviour(methods) together. If you are creating class, you are doing encapsulation.

The whole idea behind encapsulation is to hide the implementation details from users. If a data member is private it means it can only be accessed within the same class. No outside class can access private data member (variable) of other class.

This way data can only be accessed by public methods thus making the private fields and their implementation hidden for outside classes.

Example of Encapsulation in Java

How to implement encapsulation in java:

  1. Make the instance variables private so that they cannot be accessed directly from outside the class. You can only set and get values of these variables through the methods of the class.
  2. Have getter and setter methods in the class to set and get the values of the fields.

class EncapsulationDemo
{
    	private int empId;
    	private String empName;
    	private int empAge;

    	//Getter and Setter methods
    	public int getEmpId()
	{
        	return empId;
    	}

    	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 setEmpId(int newValue)
	{
        	empId = newValue;
    	}
}
public class EncapsulationTest
{
    	public static void main(String args[])
	{
        	EncapsulationDemo obj = new EncapsulationDemo();
        	obj.setEmpName("Arnav");
         	obj.setEmpAge(35);
         	obj.setEmpId(1001);
         	System.out.println("Employee Name: " + obj.getEmpName());
         	System.out.println("Employee Id: " + obj.getEmpId());
         	System.out.println("Employee Age: " + obj.getEmpAge());
    	} 
}

Output :

Employee Name: Arnav
Employee Id: 1001
Employee Age: 35

Note: In above example all the three data members (or data fields) are private(see: Access Modifiers in Java) which cannot be accessed directly. These fields can be accessed via public methods only. Fields empName, empId and empAge are made hidden data fields using encapsulation technique of OOPs.

Advantages of Encapsulation

Data Hiding Vs. Encapsulation

People often consider encapsulation as data hiding, but that’s not entirely true.

Encapsulation refers to the bundling of related fields and methods together. This allows us to achieve data hiding. Encapsulation in itself is not data hiding.

Another Example of Encapsulation


class Account 
{  
	//private data members  
	private long acc_no;  
	private String name,email;  
	private float amount; 
 
	//public getter and setter methods  
	public long getAcc_no() 
	{  
    		return acc_no;  
	}  
	public void setAcc_no(long acc_no) 
	{  
    		this.acc_no = acc_no;  
	}  
	public String getName() 
	{  
    		return name;  
	}  
	public void setName(String name) 
	{  
    		this.name = name;  
	}  
	public String getEmail() 
	{  
    		return email;  
	}  
	public void setEmail(String email) 
	{  
    		this.email = email;  
	}  
	public float getAmount() 
	{  
    		return amount;  
	}  
	public void setAmount(float amount) 
	{  
    		this.amount = amount;  
	}   
}

//A Java class to test the encapsulated class Account.  

public class TestEncapsulation 
{  
	public static void main(String[] args) 
	{  
    		//creating instance of Account class  
    		Account acc=new Account();  
    		//setting values through setter methods  
    		acc.setAcc_no(7560128450L);  
    		acc.setName("Anvar Jasmit");  
    		acc.setEmail("anvarjasmit@codemistic.in");  
    		acc.setAmount(400000f);  
    		//getting values through getter methods  
    		System.out.println(acc.getAcc_no()+" "+acc.getName()+" "+acc.getEmail()+" "+acc.getAmount());  
}  
}  

Output :

7560128450 Anvar Jasmit anvarjasmit@codemistic.in 400000.0