Encapsulation in Java

Last Updated : 29 Aug, 2026

Encapsulation in Java is an object-oriented programming concept that bundles data and the methods that operate on that data into a single unit, such as a class. It also helps control access to the object's internal state by restricting direct access and providing controlled ways to read or modify it.

  • Usually uses private fields to restrict direct access.
  • Provides controlled access through methods such as getters and setters when required.
  • Allows validation and other rules to be applied before changing data.
Encapsulation
Encapsulation

How Encapsulation is Achieved in Java

Encapsulation is commonly implemented by:

  • Declaring fields as private.
  • Providing public or appropriately accessible methods to read or modify the fields.
  • Adding validation inside methods when necessary.
  • Keeping the internal implementation hidden from code outside the class.

Rules:

  • Declare data as private: Hide the class data so it cannot be accessed directly from outside the class.
  • Use getters and setters: Keep variables private and provide public getter and setter methods for controlled access and safe modification, often with validation.
  • Apply proper access modifiers: Use private for data hiding and public for methods that provide access.
Java
class Programmer {

    private String 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());
    }
}

Output
Name=> Geek

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.

Advantages of Encapsulation

  • Protects an object's internal data from direct access.
  • Allows validation before data is modified.
  • Makes code easier to maintain and modify.
  • Keeps related data and methods together in one class.
  • Reduces dependency on the internal implementation of a class.
  • Provides better control over how an object's state can be accessed or changed.

Limitations of Encapsulation

  • Can increase the amount of code when many accessor methods are required.
  • May add unnecessary complexity for very simple classes.
  • Requires careful design to decide which data and operations should be exposed.
  • Excessive use of getters and setters can expose more of a class's internal state than necessary.

Data Hiding vs Encapsulation

AspectData HidingEncapsulation
MeaningRestricts direct access to internal dataBundles data and related methods into a class
Main focusControlling access to dataOrganizing data and behavior together
PurposeProtects internal state from unwanted direct accessProvides a controlled and well-defined interface
Commonly achieved usingAccess modifiers such as privateClasses, access control, and methods
ScopeMainly concerned with restricting accessBroader concept that includes bundling and access control
Exampleprivate int balance;class Account { private int balance; void deposit() {...} }
Comment