Encapsulation in Java

In this tutorial, we will learn about encapsulation in Java, one of the fundamental parts of object-oriented programming. We will cover the concept as well as learn how it is implemented using Java.

Encapsulation in Java

Encapsulation is a concept in Java that brings related-code together and wraps it as a single unit. It is a way of keeping things together that are closely related to each other.

encapsulation in java

In Java, the concept of Encapsulation bundles variables and methods within a single class. These variables and methods are related to each other in some way. This way a class has all the things that are related to that class.

Why do we need Encapsulation in Java?

  • To keep the code clean and organized
  • To have better access control over the members who are encapsulated
  • To understand the code in a better way

Let’s better understand the concept with a little example.

class MyBankAccount {
    private int acc_number;
    private int acc_balance;

    public MyBankAccount(int acc_number, int acc_balance) {
        this.acc_number = acc_number;
        this.acc_balance = acc_balance;
    }

    public int printAccountBalance() {
        System.out.println("Balance: " + acc_balance);
    }

    public int printAccountNumber() {
        System.out.println("Account Number: " + acc_number);
    }

    public void depositMoney(int money) {
        acc_balance = acc_balance + money;
    }
}

In the above example, we have a class MyBankAccount that has a constructor, 2 variables, and 3 methods. These members of the class are related to each other so they reside in the same class. The class MyBankAccount is encapsulating or wrapping the class contents together, and this entire code acts as a single unit.

Now just by looking at the class name i.e “MyBankAccount”, we can assume that it may have an account balance and an account number (that it has as variables).

Bank Accounts are related to monetary transactions like depositing money are printing account balances, etc. These transactions are carried with methods according to the above class. As we can see this has improved readability but also it has maintained the code in a better manner.

Suppose we had 100 classes and each class had like 5 to 10 methods and the nearly same amount of variables, then it would have been easier to for us to find certain methods or variables just by assuming which class it may be related to.

How is the concept of Encapsulation helping us?

Well, there are several benefits of encapsulation in programming, but we don’t realize it until we see it in action. In today’s world, everything is digital and software-based in some or another way. We have so many programming languages and so much code is being generated to develop software or programming libraries. All these have encapsulation in some or another way. Without encapsulation, it would be a mess to do anything in programming (especially in software industries).

Encapsulation in Java,

  • Helps us organize code in a better and understandable manner.
  • Makes us deal with a large amount of code with less effort to maintain it.
  • Reduces the complexity of managing the code.
  • Decouples the code
  • Increases readability

What if there was no encapsulation?

There are programming languages that have no concept of encapsulation. One of them is the C programming language. It doesn’t have any concept of encapsulation. There can be code spread out in files and each file can have any variable or function that may or may not be related to each other. This creates confusion in managing code and increases complexity.

Without encapsulation,

  • There can be disorganized or jumbled code
  • Increases complexity in maintaining code
  • Debugging code is difficult
  • Decreases readability

I hope now you have a better understanding of encapsulation and its use in Java programming. To learn more concepts and programming practices stay in touch. Thank you for reading.