> For the complete documentation index, see [llms.txt](https://sec88.0x88.online/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://sec88.0x88.online/programming/java/java-oop-principles/encapsulation.md).

# Encapsulation

Encapsulation is one of the key features of object-oriented programming. Encapsulation refers to the bundling of fields and methods inside a single class, It prevents outer classes from accessing and changing fields and methods of a class. This also helps to achieve **data hiding**.

**Example 1: Java Encapsulation**

```java
class Area {
  // Fields to calculate area
  int length;
  int breadth;

  // Constructor to initialize values
  Area(int length, int breadth) {
    this.length = length;
    this.breadth = breadth;
  }

  // Method to calculate area
  public void getArea() {
    int area = length * breadth;
    System.out.println("Area: " + area);
  }
}

class Main {
  public static void main(String[] args) {
    // Create object of Area, pass values of length and breadth
    Area rectangle = new Area(5, 6);
    rectangle.getArea();
  }
}
```

**Why Encapsulation?**

1. **Organization and Readability:**
   * Encapsulation helps organize related fields and methods within a class, improving code readability.
2. **Control over Data:**
   * It provides control over data values, allowing for logic implementation, such as in setter methods.
3. **Getter and Setter Methods:**
   * Getter and setter methods offer controlled access to class fields.
4. **Decoupling Components:**
   * Encapsulation supports the development of decoupled components, enabling independent testing and debugging.
5. **Data Hiding:**
   * Data hiding is achieved by making fields private, restricting unauthorized access from outside the class.

**Data Hiding Example:**

```java
class Person {
  // Private field
  private int age;

  // Getter method
  public int getAge() {
    return age;
  }

  // Setter method
  public void setAge(int age) {
    this.age = age;
  }
}

class Main {
  public static void main(String[] args) {
    // Create an object of Person
    Person p1 = new Person();

    // Change age using setter
    p1.setAge(24);

    // Access age using getter
    System.out.println("My age is " + p1.getAge());
  }
}
// Output:
// My age is 24
```

In the above example, we have a `private` field age. Since it is `private`, it cannot be accessed from outside the class.

In order to access age, we have used `public` methods: `getAge()` and `setAge()`. These methods are called getter and setter methods.

Making age private allowed us to restrict unauthorized access from outside the class. This is **data hiding**.

If we try to access the age field from the Main class, we will get an error.

```java
// error: age has private access in Person
p1.age = 24;
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://sec88.0x88.online/programming/java/java-oop-principles/encapsulation.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
