encapsulation in java

encapsulation in java

Encapsulation is one of the four fundamental object-oriented programming (OOP) concepts, along with inheritance, polymorphism, and abstraction. It refers to the bundling of data (attributes) and methods (behaviors) that operate on the data into a single unit, known as a class. Encapsulation hides the internal state of an object from the outside world and restricts direct access to the data, allowing access only through publicly exposed methods. Here are the key aspects of encapsulation:

encapsulation in java

Table of Contents

  • 1. Data Hiding: Encapsulation hides the internal implementation details of an object’s data from external access. The data members (attributes) of a class are typically declared as private, preventing direct modification or access from outside the class.
  • 2. Access Modifiers: Access modifiers such as ‘public’, ‘private, protected’, and default (no modifier) are used to control the visibility and accessibility of class members. By declaring data members as private, they can only be accessed within the same class, ensuring data security and integrity.
  • 3. Getters and Setters: Encapsulation promotes the use of getter and setter methods to read and modify the state of an object, respectively. Getter methods (also known as accessor methods) provide read-only access to the private data members, while setter methods (also known as mutator methods) enable controlled modification of the data.
  • 4. Data Validation: Encapsulation allows the implementation of data validation logic within setter methods to enforce constraints and ensure the consistency and correctness of the data. This helps prevent invalid or inconsistent states of objects.
  • 5. Information Hiding: Encapsulation hides unnecessary implementation details and exposes only the essential interface to interact with the object. This promotes information hiding, reducing the complexity and dependencies of client code on the internal structure of objects.
  • 6. Modularity and Maintainability: Encapsulation enhances modularity by grouping related data and behavior into a cohesive unit (class), making the codebase easier to understand, maintain, and refactor. Changes to the internal implementation of a class do not affect external code that uses the class’s interface.

Example of encapsulation
public class Student {
    private String name;
    private int age;

    // Getter methods
    public String getName() {
        return name;
    }

    public int getAge() {
        return age;
    }

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

    public void setAge(int age) {
        if (age >= 0) { // Validate age
            this.age = age;
        } else {
            System.out.println("Invalid age");
        }
    }
}

In this example, the ‘name’ and ‘age’ attributes of the ‘Student’ class are declared as private to encapsulate them. Getter and setter methods (‘getName’, ‘setName’, ‘getAge’, ‘setAge’) provide controlled access to these attributes, ensuring data encapsulation and validation. Client code interacts with the ‘Student’ objects using these public methods, preserving encapsulation and data integrity.