Encapsulation in Java | technical-arbaab

Encapsulation in Java

It means binding or wrapping up the properties and behavior in a single unit is generally called as encapsulation in java, it is primarily used for data hiding and security purposes we usually achieve data hiding by using packages (folder) and access modifiers (public, private, default and static) don' t worry we already discussed about access modifiers in a separate article you can click on it if you want to read about that for now I am assuming you have a basic idea about packages and access modifiers in java and with that let's move forward with understanding encapsulation in detail although we already understood this meaning of encapsulation in our oops article but there we didn't discussed about this in detail when I am saying detail we didn't discuss how it is implemented in java but here we will see in detail how it is implemented in java with the help of code snippet so that you will get the complete clarity around this topic.

How to achieve encapsulation in java

Example 01-

Let' see this with the help of a very basic Student example please have a look first on the code then I will clarify you step by step what is happening in the code snippet but first please go through the code example carefully.


// Encapsulation example with very simple logic

class Student {
    // Private variable (data hiding)
    private String name;

    // Method to set name (controlled access)
    public void setName(String n) {
        name = n; // assigning value to private variable
    }

    // Method to display name
    public void displayName() {
        System.out.println("Student name is: " + name);
    }
}

// Main class
public class Main {
    public static void main(String[] args) {
        // Create object
        Student s1 = new Student();

        // Set name using method (not directly)
        s1.setName("John");

        // Display name using method
        s1.displayName();
    }
}


Output:

Student name is: John

let me clarify you what is actually happening in the above code example we created a class called Student and we created a private variable name for data hiding purpose and also created a public method setName() for assigning value to that variable and one method for displaying the name and in the main method we are creating a object of Student class and setting the name of Student by the method not directly because name is private it is only accessed directly inside Student class only achieving data hiding here and then displaying the name simply hope that makes some sense to you.

Example 02-

Let's see one another example for understanding how we are achieving data hiding using encapsulation in java.

// Encapsulation example with simple logic
class Car {
    // Private variables (data hiding)
    private String carName;
    private int carYear;

    // Method to set car details (controlled access)
    public void setCarDetails(String name, int year) {
        carName = name;
        carYear = year;
    }

    // Method to display car details
    public void displayCarDetails() {
        System.out.println("Car Name: " + carName);
        System.out.println("Car Year: " + carYear);
    }
}

// Main class
public class Main {
    public static void main(String[] args) {
        // Create object
        Car car1 = new Car();

        // Set car details using method
        car1.setCarDetails("Toyota", 2022);

        // Display car details using method
        car1.displayCarDetails();
    }
}


Output:
Car Name: Toyota
Car Year: 2022

Let me explain you so quick what is actually happening in the above code snippet there's a class called car having two private variables carName and carYear for restricting the access and there we created two public methods  setCarDetails() and displayCarDetails() so that we can access these from our Main class then we can use this for setting car details using this method only hiding data here without this method we can't directly access those two carName and carYear variable hope you are getting and finally in the main method we created the object in our Main class and setting and displaying car details finally achieving data hiding simply.


please see the below diagram for better clarity how this is working:



Comments