Ugenz Academy/Tutorial

Encapsulation

3 min read488 words

Reading progress

Keep learning

Encapsulation in Java

Encapsulation is one of the four main principles of Object-Oriented Programming (OOP). It means wrapping data (variables) and methods into a single unit (class) and controlling direct access to the data.

In Java, encapsulation is commonly achieved by:

  • Declaring variables as private
  • Providing public getter and setter methods to access and modify the data

Syntax

class ClassName { private dataType variable; public dataType getVariable() { return variable; } public void setVariable(dataType variable) { this.variable = variable; } }

Example

class Student { private String name; private int age; public void setName(String name) { this.name = name; } public String getName() { return name; } public void setAge(int age) { this.age = age; } public int getAge() { return age; } } class Main { public static void main(String[] args) { Student s = new Student(); s.setName("Anuradha"); s.setAge(22); System.out.println("Name: " + s.getName()); System.out.println("Age: " + s.getAge()); } }

Output

Name: Anuradha Age: 22

Here, name and age are declared private, so they cannot be accessed directly from outside the Student class. The get and set methods provide controlled access.

Getter and Setter

Getter

A getter is a method used to retrieve or read the value of a private variable.

public String getName() { return name; }

Setter

A setter is a method used to assign or modify the value of a private variable.

public void setName(String name) { this.name = name; }

Advantages of Encapsulation

  1. Data Security – Protects data from direct access.
  2. Data Hiding – Hides internal implementation details.
  3. Controlled Access – Allows controlled reading and modification of data.
  4. Maintainability – Makes code easier to modify and maintain.
  5. Flexibility – Internal implementation can be changed without affecting other code.
  6. Validation – Setters can validate data before storing it.

Example of Validation

class Student { private int age; public void setAge(int age) { if (age >= 0 && age <= 100) { this.age = age; } else { System.out.println("Invalid age"); } } public int getAge() { return age; } }

Encapsulation vs Data Hiding

EncapsulationData Hiding
Combines data and methods into a class.Restricts direct access to data.
Achieved through classes and access control.Commonly achieved using private.
Focuses on bundling and controlled access.Focuses mainly on protecting data.

Key Points

  • Encapsulation is a major OOP concept.
  • Use private variables to restrict direct access.
  • Use getters and setters for controlled access.
  • Encapsulation improves security and maintainability.
  • It allows validation before modifying data.

Conclusion

Encapsulation protects the internal data of an object by restricting direct access and providing controlled access through methods. It makes Java programs more secure, organized, flexible, and maintainable.

Keep learning. Keep building. Keep growing.

© 2026 Ugenz Academy