Last Updated : 06 Aug, 2025
In Java, encapsulation is one of the core concepts of Object Oriented Programming (OOP) in which we bind the data members and methods into a single unit. Encapsulation is used to hide the implementation part and show the functionality for better readability and usability. The following are important points about encapsulation.
Example: Below is a simple example of Encapsulation in Java.
Java
// Java program demonstrating Encapsulation
class Programmer {
private String name;
// Getter and Setter for name
// Getter method used to get the data
public String getName() { return name; }
// Setter method is used to set or modify the data
public void setName(String name) { this.name = name; }
}
public class Geeks {
public static void main(String[] args) {
Programmer p = new Programmer();
p.setName("Geek");
System.out.println("Name=> " + p.getName());
}
}
Explanation: In the above example, we use the encapsulation and use getter (getName) and setter (setName) method which are used to show and modify the private data. This encapsulation mechanism protects the internal state of the Programmer object and allows for better control and flexibility in how the name attribute is accessed and modified.
Uses of Encapsulation in JavaUsing encapsulation in Java has many benefits:
This image below demonstrates the concept of encapsulation, where a class (represented by the capsule) hides its internal data (variables) and only exposes a controlled interface (encapsulation).
Encapsulation Implementation of Java EncapsulationIn Java, encapsulation is implemented by declaring instance variables as private, restricting direct access. Public getter methods retrieve variable values, while setter methods modify them, enabling controlled access. This approach allows the class to enforce data validation and maintain a consistent internal state, enhancing security and flexibility.
Encapsulation is defined as the wrapping up of data under a single unit. It is the mechanism that binds together code and the data it manipulates. Another way to think about encapsulation is, that it is a protective shield that prevents the data from being accessed by the code outside this shield.
Example 1: Here is another example of encapsulation in Java.
Java
class Person {
// Encapsulating the name and age
// only approachable and used using methods defined
private String name;
private int age;
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; }
}
// Driver Class
public class Geeks {
// main function
public static void main(String[] args)
{
// person object created
Person p = new Person();
p.setName("Sweta");
p.setAge(25);
// Using methods to get the values from the
// variables
System.out.println("Name: " + p.getName());
System.out.println("Age: " + p.getAge());
}
}
Name: Sweta Age: 25
Explanation: Here, the encapsulation is achieved by restricting direct access to the name and age fields of the Person class. These fields are marked as private and can only be accessed or modified through public getter and setter methods (getName(), setName(), getAge(), setAge()). This approach ensures data hiding and maintains control over the values of these fields.
Example 2: In this example, we use abstraction to hide the implementation and show the functionality.
Java
class Area {
private int l; // this value stores length
private int b; // this value stores breadth
// constructor to initialize values
Area(int l, int b)
{
this.l = l;
this.b = b;
}
// method to calculate area
public void getArea()
{
int area = l * b;
System.out.println("Area: " + area);
}
}
public class Geeks {
public static void main(String[] args)
{
Area rect = new Area(2, 16);
rect.getArea();
}
}
Explanation: In the above example, Area class and inside the class we create a constructor which takes two parameters l (length) and b (breath) and a getArea() method to calculate the area of rectangle. This method shows the abstraction where the implementation (Calculating area) is hidden and functionality is showing (Area of rectangle).
Example 3: The program to access variables of the class Geeks is shown below:
Java
class Encapsulate {
// private variables declared
// these can only be accessed by
// public methods of class
private String geekName;
private int geekRoll;
private int geekAge;
// get method for age to access
// private variable geekAge
public int getAge() { return geekAge; }
// get method for name to access
// private variable geekName
public String getName() { return geekName; }
// get method for roll to access
// private variable geekRoll
public int getRoll() { return geekRoll; }
// set method for age to access
// private variable geekage
public void setAge(int newAge) { geekAge = newAge; }
// set method for name to access
// private variable geekName
public void setName(String newName)
{
geekName = newName;
}
// set method for roll to access
// private variable geekRoll
public void setRoll(int newRoll) { geekRoll = newRoll; }
}
// Main Class
public class Geeks
{
public static void main(String[] args)
{
Encapsulate o = new Encapsulate();
// setting values of the variables
o.setName("Geeky");
o.setAge(19);
o.setRoll(51);
// Displaying values of the variables
System.out.println("Geek's name: " + o.getName());
System.out.println("Geek's age: " + o.getAge());
System.out.println("Geek's roll: " + o.getRoll());
// Direct access of geekRoll is not possible
// due to encapsulation
// System.out.println("Geek's roll: " +
// obj.geekName);
}
}
Geek's name: Geeky Geek's age: 19 Geek's roll: 51
Explanation: Here, the class Encapsulate keep its variable private, it simply means that they can not be accessed directly from outside the class. To get the values of these variables we need to use public methods. This way the data can only be accessed or changed through these methods.
Example 4: Let us go through another example, to understand the concept better.
Java
class Account {
// Private data members (encapsulated)
private long accNo; // Account number
private String name;
private String email;
private float amount;
// Public getter and setter methods (controlled access)
public long getAccNo() { return accNo; }
public void setAccNo(long accNo) { this.accNo = accNo; }
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; }
}
// Main Class
public class Geeks
{
public static void main(String[] args)
{
// Create an Account object
Account acc = new Account();
// Set values using setter methods (controlled access)
acc.setAccNo(90482098491L);
acc.setName("ABC");
acc.setEmail("abc@gmail.com");
acc.setAmount(100000f);
// Get values using getter methods
System.out.println("Account Number: " + acc.getAccNo());
System.out.println("Name: " + acc.getName());
System.out.println("Email: " + acc.getEmail());
System.out.println("Amount: " + acc.getAmount());
}
}
Account Number: 90482098491 Name: ABC Email: abc@gmail.com Amount: 100000.0
Explanation: Here, we are using encapsulation and hiding all the details inside the Account class. We can not access all the details from outside the class so to access all the details we need to create getter and setter methods so that we can easily get and update the details. In the main method, an Account object is created so that values can be set using the setter and get the value using getter.
Advantages of EncapsulationThe advantages of encapsulation are listed below:
The disadvantages of encapsulation are listed below:
The common mistakes that can occur when working with encapsulation in Java are listed below:
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4