Encapsulation in Java

Encapsulation in Java is one of the fundamental concepts of object-oriented programming (OOP).

It refers to the bundling of data (attributes or fields) and methods (functions or procedures) that operate on the data into a single unit called a class.

Encapsulation allows us to control access to the data, ensuring that the internal state of an object is safe from outside interference and manipulation.

Overview of Encapsulation in Java:

  • Data Hiding: Encapsulation hides the internal state of an object from direct access by other classes. This prevents the accidental modification of data and maintains the integrity of the object's state.

  • Access Control: Access to the data members (attributes) of a class is controlled using access modifiers such as private, public, protected, etc. This allows classes to enforce restrictions on how data is accessed and modified.

  • Information Hiding: Encapsulation also hides the implementation details of a class, exposing only the necessary interface to interact with the class. This promotes loose coupling and reduces the dependency of other classes on the internal structure of the encapsulated class.

Benefits of Encapsulation in java:

  • Enhanced Security: It prevents unauthorized access to data, reducing the risk of data corruption or misuse.

  • Improved Maintainability: It allows changes to the internal implementation of a class without affecting the external code that uses the class. This promotes code maintainability and facilitates code evolution.

  • Modularity: Encapsulation promotes modular design by encapsulating related data and behaviours into cohesive units (classes), making the codebase easier to understand, modify, and extend.

  • Code Reusability: Encapsulated classes can be reused in different parts of the application or other applications, leading to increased code reusability and efficiency.

Here's how encapsulation works in Java:

Example:

public class Employee {

    // Private data members
    private String name;
    private int age;
    
    // Public constructor
    public Employee(String name, int age) {
        this.name = name;
        this.age = age;
    }
    
    // Public getter method for name
    public String getName() {
        return name;
    }
    
    // Public setter method for name
    public void setName(String name) {
        this.name = name;
    }
    
    // Public getter method for age
    public int getAge() {
        return age;
    }
    
    // Public setter method for age
    public void setAge(int age) {
        if (age > 0) {
            this.age = age;
        } else {
            System.out.println("Age cannot be negative.");
        }
    }

}
  • "name" and "age" are private data members of the "Employee" class.

  • getName() and setName() are public methods for accessing and modifying the name attribute.

  • getAge() and setAge() are public methods for accessing and modifying the age attribute, with a simple validation to prevent negative age values.

Note:

Encapsulation helps in achieving data hiding, abstraction, and modularity in your Java programs, which leads to better organization, maintenance, and security.