Encapsulation in Object oriented programming

Kavindu Sandaruwan
3 min readJan 11, 2024

--

Encapsulation is another key concept of OOP that allows hiding the internal state and behavior of an object from outside access. It also helps to achieve data protection and validation. In this blog post, we will learn what encapsulation is, how to implement it in Java, and what are its benefits.

What is encapsulation?

The word encapsulation means "to enclose" or "to wrap". In OOP, encapsulation refers to the wrapping of data (fields) and methods (code) that operate on that data within a single unit, which is called a class in Java. Encapsulation ensures that the data of a class is hidden from other classes and can only be accessed through the public methods of that class. These public methods are called getters and setters, which are used to retrieve and modify the values of the private fields, respectively. By using getters and setters, the class can enforce its own rules and logic on the data, such as data validation, data consistency, and data security.

How to implement encapsulation in Java?

In Java, encapsulation is achieved by declaring the fields of a class as private, which means they can only be accessed within the class. To allow outside access to the fields, public methods called getters and setters are defined, which have the same name as the fields but with a prefix of get or set. For example, we can have a class called Student that has two private fields: name and age, and four public methods: getName, setName, getAge, and setAge.

Now, we can create an object of Student and use the getters and setters to access and modify the values of the private fields. For example:

Output:

```
Name: Alice
Age: 20
```

What are the benefits of encapsulation in Java?

Encapsulation has several benefits for OOP, such as:

- It provides data hiding, which means that the internal details of a class are hidden from outside access and manipulation. This prevents unauthorized or accidental changes to the data and ensures data integrity and security.
- It provides data abstraction, which means that the class exposes only a public interface that defines its behavior and functionality, without revealing its implementation details. This makes the code more readable, understandable, and maintainable.
- It provides data control, which means that the class can implement its own logic and rules on the data, such as data validation, data conversion, data calculation, etc. This ensures data consistency and accuracy.
- It supports the principle of loose coupling, which means that the classes are less dependent on each other and can be modified or extended easily without affecting other classes.

Conclusion

In this blog post, we have learned what encapsulation is, how to implement it in Java, and what are its benefits. Encapsulation is a powerful concept that allows us to create more secure, reliable, and modular code that can handle various situations and scenarios. We hope that this blog post has helped you understand and appreciate the concept of encapsulation in OOP using Java.

--

--