Let's learn java programming language with easy steps. This Java tutorial provides you complete knowledge about java technology.

Wednesday, 24 May 2017

Encapsulation In Java


encapsulation in java

Java encapsulation is used for wrapping the data and method together into a single unit, its known as encapsulation in java.

In other words, By the help of encapsulation we can combine the state(data member) and behavior(method) into a single unit.

Encapsulation can be achieved only class concept.

The best example of encapsulation is Java Bean class.

We can create encapsulated class in java, if we make all the data member(state) as private and use setter and getter method to set and get the data in it.

Encapsulation is also known as data hiding in java.

The real life example of encapsulation is medicine capsule  because capsule contains several other medicine.

Advantage of Encapsulation in Java

  1. The first advantage of using encapsulation in java is to secure the data from other methods because when we make the data(state) private then these data only use within the same class , these data cannot be accessible outside of a class.
  2. The second advantage of using encapsulation in java is to hide the internal details from the user so that user would not predict what's happening behind the program.
  3. It can be maintain and reuse easily.
  4. It provides abstraction between objects and its client.

Example of Encapsulation in Java

This is simple example of java encapsulation, Here in this example we will take three field of employee class like salary, age, name and its setter and getter methods. First we will set value and then get value of employee class field.

public class EncapsulationExample

//Declaring private field
private String name;
private int age;
private int salary;

//Declaring setter and getter methods
public String getName()
return name;
public void setName(String name)
this.name = name;

public int getAge()


return age;


public void setAge(int age)


this.age = age;


public int getSalary()
return salary;
public void setSalary(int salary)

this.salary = salary;



class SetAndGetValue


public static void main(String args[])


EncapsulationExample e = new EncapsulationExample();

//set the value

//get the value
System.out.println("Employee name "+e.getName());
System.out.println("Employee age "+e.getAge());
System.out.println("Employee salary "+e.getSalary());

Save : EncapsulationExample
Compile : EncapsulationExample
Run : SetAndGetValue  

output : Employee name siya
              Employee age 28
              Employee salary 12000

Differences Between Abstraction and Encapsulation inJava

There are some differences between abstraction and encapsulation in java.

  • You can use abstraction using java interface and abstract class.
  • In design level, abstraction solves the problem.
  • Abstraction means hiding implementation details or complexity by the help of abstract class or interface.
  • Abstraction basically used to hide the internal working .
  • You can implement encapsulation by using access modifiers(private, protected, public).
  • In implementation level, encapsulation solves the problem.
  • Encapsulation means data hiding or information hiding, Which is used to secure the data from outside world.
  • Encapsulation basically used to secure the data from outside access .In encapsulation, we can bind the data and method in single unit and make private all the data for security reason. 



Facebook Page Likes

Follow javatutorial95 on twitter

Popular Posts