Encapsulation In Java- Quick Pocket Guide For Every Newbie!

For any new developer, it is necessary to learn all about the fundamental new concepts of programming language. Four major fundamental concepts in Java are inheritance, polymorphism, encapsulation, and abstraction. By the title of this blog, one can easily make out the topic of our discussion today. So if you are ready to broaden your understanding then keep reading!

"Today let us expand our understanding about encapsulation in Java!"

A Brief Introduction To Encapsulation In Java!

We can define encapsulation as the wrapping up of data into a single unit. But technically, it means accessing private data members through public member functions. One of the simplest examples of the same is a pojo/data class.

If the technical definitions are bouncing right from your head, then we have a better way to clear the concept.

Simplifying Encapsulation With An Example!

Let us say you have a bank account in your name where the variable accountBalance is made public.

If this happens anyone can have a look at your bank account balance. This means anyone has easy access to all of your private information.

Would you like it? Obviously not!

I am sure this blunder must be giving you the goosebumps.

In order to avoid the same, we make our variable account Balance as private.

Now if you want to access your own account data you need to input the user id and password. Once you enter these details, you can get the details of the account which are declared as private.

How You Can Achieve Encapsulation In Java?

Here's how-

1# Declare the variables of class as private.
2# Make sure to provide public sector and getter methods in order to modify and view the variables value.

Understanding Encapsulation With The Help Of Codes!

Here is a simple explanation from a coding point of view, take a good look-

public class BankAccount {

private String accountNumber = "";
private double accountBalance = 0.00;
private String accountHolderName = "";

public void setAccountBalance (double accountBalance){
this.accountBalance = accountBalance;

public Double getAccountBalance (){
return accountBalance;

public void setAccountNumber (String accountNumber){
this.accountNumber = accountNumber;

public String getAccountNumber() {
return accountNumber;

public void setAccountHolderName (String accountHolderName){
this.accountHolderName = accountHolderName;

public String getAccountHolderName (){
return accountHolderName;

public void getAccountDetails(long userId , long password){

if(userId == 12345 && password == 1234567890){
System.out.println(getAccountHolderName() + " " + getAccountNumber() + " " + getAccountBalance() );
System.out.println("wrong credentials");


public static void main(String [] args){

BankAccount ba = new BankAccount();
ba.setAccountHolderName("Test Name");

ba.getAccountDetails(12345 , 1234567890);



What Are The Major Advantages Of Encapsulation?

Take a look at the top perks-

  1. A class can have total control over what is stored in its fields.
  2. The fields of a class can be made read-only or write-only.
  3. It makes applications simple.
  4. Because of it, one can debug the applications easily.
  5. One can easily hide the implementation of the program definition.
  6. It makes programming flexible.
  7. You can reuse the code and implement new requirements.
  8. Helps in achieving loose coupling.

In A Nutshell

Now you must have having a clear understanding of Encapsulation in Java. But if you still have doubts then feel free to reach out to us for the same. You can also mention your queries in the comment section present below. We would be delighted to sort out all your queries.

Always remember that as a new coder, you need to strengthen your foundational concepts. Only then you would be able to handle big projects with a high complexity level. So keep practicing and keep coding!

Do you have more topics that you want to ace as a coder? If you do, then let us know. But until then, stay tuned to this space for more exciting tech-based information from all around the world.

More articles: Java


No responses found. Be the first to comment...

  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name: