Encapsulation In Java

Encapsulation is the simple act of coupling an object’s state (fields) and behavior (methods). Encapsulation is done when you create a class. This article will show you how to encapsulate code in a Java program. If you’re searching for a real-world encapsulation example, check out this guide: OOPs concepts illustrated with examples from everyday life.

Returning to the subject

What is encapsulation?

Encapsulation’s whole purpose is to shield consumers from the implementation’s finer points. A data member can only be accessible by other members of the same class if it is private. No other class may access a member’s (variable’s) private data.

The outside class, however, may access the private data fields through public methods if we build up public getter and setter methods to update (for example, void setSSN(int ssn)) and read (for example, int getSSN()) the private data fields.

By restricting access to data to public methods exclusively, private fields and their implementation become hidden to classes outside of the current one. Encapsulation is often known as data concealing because of this. To further comprehend this notion, let’s look at an example.

Example of Encapsulation in Java

How to use Java’s encapsulation feature:
1) Make the instance variables private so that no one outside the class may access them directly. Only the class’s methods may be used to set and get the values of these variables.
2) In the class, there should be getter and setter methods for setting and retrieving the values of the fields.

class EncapsulationDemo{
    private int ssn;
    private String empName;
    private int empAge;

    //Getter and Setter methods
    public int getEmpSSN(){
        return ssn;

    public String getEmpName(){
        return empName;

    public int getEmpAge(){
        return empAge;

    public void setEmpAge(int newValue){
        empAge = newValue;

    public void setEmpName(String newValue){
        empName = newValue;

    public void setEmpSSN(int newValue){
        ssn = newValue;
public class EncapsTest{
    public static void main(String args[]){
         EncapsulationDemo obj = new EncapsulationDemo();
         System.out.println("Employee Name: " + obj.getEmpName());
         System.out.println("Employee SSN: " + obj.getEmpSSN());
         System.out.println("Employee Age: " + obj.getEmpAge());


Employee Name: Mario
Employee SSN: 112233
Employee Age: 32

All three of the data members (or fields) in the aforementioned example are private (see: Access Modifiers in Java) and cannot be accessed directly. Only public methods may access these fields. The OOPs encapsulation approach was used to make the fields empName, ssn, and empAge into hidden data fields.

Advantages of encapsulation

Improvements in flexibility, maintainability, and reuse include, for instance, the ability to modify the implementation code for the functions void setEmpName(String name) and String getEmpName() at any moment in the code above. Outside classes would still be able to access the private field empName using the same methods (setEmpName(String name) and getEmpName()) since the implementation is simply hidden for them. As a result, the code may be updated at any moment without causing the classes that utilize it to crash. This enhances the underlying class’s ability to be reused.
If the class doesn’t specify setter methods or getter methods, the fields may be declared read-only or write-only, respectively. For instance, if we have a field (or variable) that we don’t want to be updated, we can simply specify the variable as private and only need to create the get method for that variable rather than the set and get methods. An outside class cannot change the value of the field since the set function is missing.
The user would be unaware of what is happening behind the scenes. They would simply be aware of how to call the set method to change a field and the get method to read it, but they would have no idea what these set and get methods really perform. OPs.


Related Articles

Leave a Reply

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

Back to top button