Concept of Encapsulation in Java

Object-Oriented Programming, also known as OOP, is one of Java’s essential concepts that has leveraged its power and ease of usage. To become an expert Java developer, you must get perfect control over the various Java OOP concepts like Inheritance, Abstraction, Encapsulation, and Polymorphism. Through this article, I will give you insight into one of the essential concepts of OOPs, i.e., Encapsulation in Java, and how you can implement it.

Encapsulation in java

Encapsulation in java is one of the fundamental concepts of Object-Oriented Programming. It refers to the bundling of variables and methods inside a single class. The whole idea of encapsulation is to hide the implementation details from users. If data is private, it means you can only access it within the same class. An outside class cannot access private data members (variables) of other classes. This way, private data can only be accessed using public methods, making the private fields and their implementation hidden from outside classes. This is the reason why encapsulation is known as data hiding.

To give you an analogy, encapsulation works like a capsule where the medicine inside the pill would be data and methods while the outer covering would be the hard shell capsule. 

Let’s see an example of a bank to understand it better; we don’t want the third party to access your personal information because it will compromise your life savings. So we declare your card number and password as a private access modifier.

Output:

Since the card number and password are private, they are encapsulated inside the class bank and cannot be accessed from outside classes. This helps us make the program more robust to attacks from outside and prevents unnecessary mistakes.

But there are cases where we need to retrieve or write the data from the encapsulated class. For example, if you want to change your password or lose your old card and get assigned a new one. To tackle this problem, we use getter and setter methods.

Getter and Setter Methods

In Java, getter and setter are two traditional methods used to retrieve and write values to a variable. As the name suggests, the getter method gets or retrieves the data while the setter method sets or writes to the variable. 

The general naming convention is using the name getX() for the getter method and setX() for the setter method of a field X.

Let’s continue our bank’s example, but this time we will update the password and retrieve the card number and password.

Output:

This time we were successfully able to retrieve the data and also change the password. Because we used public methods accessible to other classes, we can limit the amount of information we want others to access and keep the complexity at minimal.

Abstraction vs. Encapsulation

Encapsulation is misunderstood often with Abstraction. The main difference between both is:

  • Encapsulation means hiding the code and data into a singular unit to protect the data from the outside world.
  • Abstraction is used to suppress unwanted data and only give relevant data.

An example to understand this difference is a phone. The model of the phone, like iPhone 12 or Samsung Galaxy S10, would be encapsulated. In contrast, a basic structure with a USB port and circuit board would be an abstraction.

Why do we need Encapsulation in Java?

  • Security: The main advantage of encapsulation is that it enhances your data’s safety since it can only be accessed using public getters and setters methods.
  • Control: Encapsulation makes the code robust by giving the developer control over what’s stored within the member variables.
  • Flexibility: Good design approaches like encapsulation makes your code more flexible, which successively makes the code easier to change and maintain.

written by: Vijay Lalwani

reviewed by: M SASANK SAI, Bujagawni Sai Teja Goud

If you are Interested In Machine Learning You Can Check Machine Learning Internship Program
Also Check Other Technical And Non Technical Internship Programs

Leave a Comment

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