Encapsulation in JAVA

Encapsulation is a process of wrapping of data and methods in a single unit. Encapsulation is achieved in java language by the class concept.

We can call it as the process of providing controlled access to the private members of a class by the method/ function of that class.

In Encapsulation, we will make the Instance Variable private and that variable will only accessible by the method/ function of that class.

In Real Time, if you take an example of Capsule. It can contain more than two types of medicine powder and the combination of that medicine forms a single unit that works as a healer. So here the Capsule is a complete class that contains the data and methods.


Encapsulation provides the abstraction between the object and its client.

Second main Advantage is that it provides the protection against the unwanted method from accessing the private member of the class.


class Employee
private String name;  // name is private so it is only accessible by method of this class

public String getName()
return name;  
public void setName(String name){  

class Demo
public static void main(String[] args)
Employee e=new Employee();  
e.setName("Harry");  // only setName() method can Modify the data 
System.out.println(e.getName());  // ans getName() method can access the data of class Employee 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Powered by WordPress.com.

Up ↑

%d bloggers like this: