Understanding of Encapsulation in Java

By | June 7, 2018

This post talks about Understanding of Encapsulation in Java

Encapsulation is one of the four fundamental OOP concepts. The other three are-

Abstraction
Polymorphism
Inheritance

Encapsulation Concept
Encapsulation is used to keep together the implementation (code) and the data it manipulates (variables). Proper encapsulation ensures that the code and data both are safe from misuse by outside entity.

Why to use Encapsulation

Encapsulation is the process of making the fields in a class private and providing access to these fields via public methods. If a field is declared private, it cannot be accessed by anyone outside of the class, thereby hiding the fields within the class. Because of this reason, encapsulation is also referred to as data hiding.

Encapsulation in Java

Encapsulation can also be described as a protective process that prevents the code and data being randomly accessed by other code defined outside the class. Access to the data and code is tightly controlled by the access modifiers- public, protected default and private.

The major benefit of encapsulation is the ability to edit our implemented code without breaking the code of others who use our code. With this feature Encapsulation gives flexibility, maintainability and extensibility to our code.

Class is the foundation of encapsulation in Java.In a class you will have both methods and instance variables.
Since it is the code in any function or method that operates on the properties or variables, in a properly encapsulated Java classes, method should define how member variables or properties can be used.
As I said that access control to the functions or methods and the properties or variables is achieved through access modifiers (public, protected, default & private).
Each function or method or variable in the class may be marked public, protected or private in order to ensure access control. A method or variable marked as public is accessible to other classes(anywhere). Any method or variable marked as private is internal to the class.

In a properly encapsulated Java classes, you should mark only those methods as public that should be accessed by the other classes. We can get complete encapsulation in java by making properties or variables of a class private and access to them, outside the class, is provided only through public methods (getters and setters methods).
Any method having logic that is specific to that class only and is used by other methods of that class should be marked private.
Since the private members (variables or methods) of a class may only be accessed with in the class, no improper outside access is possible.

Encapsulation Java Example code

In the above program the class Employee is encapsulated as the variables are declared as private. The get methods like getId() , getName() are set as public, these methods are used to access these variables. The setter methods like setId(), setName(), are also declared as public and are used to set the values of the instance variables.

The program to access variables of the class Employee is shown below:

Key Points about Encapsulation:

1. Encapsulation is one of the OOPs feature Other are, inheritance, polymorphism and abstraction.
2. Encapsulation helping us in binding the data (instance variables) and the member methods (that work on the instance variables) of a class.
3. Encapsulation is also helps us in hiding the data or instance variables of a class from an illegal direct access.
4. Encapsulation helps us to make a flexible code which is easy to change and maintain.

Would you like to watch on YouTube:

That’s all about Understanding of Encapsulation in Java

You May Also Like:

What are JVM, JRE and JDK in Java?
Differences between Java EE and Java SE
Abstraction in Java
Understating of Polymorphism in Java
Method Overloading or Static Polymorphism in Java
Method Overriding or Dynamic Polymorphism in Java
Understating of Inheritance in Java

If you have any feedback or suggestion please feel free to drop in below comment box.

Leave a Reply

Your email address will not be published. Required fields are marked *