JAVA Tutorial



INCAPSULATION IN JAVA


Encapsulation in Java

Encapsulation is one of the four fundamental Object-Oriented Programming (OOP) concepts. It refers to the bundling of data (variables) and methods that operate on that data into a single unit called a class. Additionally, encapsulation allows restricting access to certain details of an object's implementation while providing a controlled interface.

What is Encapsulation?

In Java, encapsulation is achieved by declaring the variables of a class as private and providing public getter and setter methods to access and update the values of these variables. This helps in hiding the internal workings of an object and protecting the data from unauthorized access or modification.

Why Use Encapsulation?

  • Data Hiding: Encapsulation ensures that the internal state of an object is hidden from outside interference and misuse.
  • Control: Through getter and setter methods, we can control how the data is accessed and modified, applying validation or restrictions.
  • Flexibility and Maintenance: It is easier to maintain and modify the code since changes to the internal workings of a class do not affect external code that uses it.

How Encapsulation Works in Java

Encapsulation is implemented by using the following steps:

  1. Declare the fields of a class as private.
  2. Provide public getter and setter methods to access and update the private fields.

Example of Encapsulation

Letโ€™s see an example of encapsulation in Java. We have a class Person with private fields for the name and age. We then provide public getter and setter methods to access and update these fields.

class Person {
    // Private fields
    private String name;
    private int age;

    // Getter method for name
    public String getName() {
        return name;
    }

    // Setter method for name
    public void setName(String name) {
        this.name = name;
    }

    // Getter method for age
    public int getAge() {
        return age;
    }

    // Setter method for age
    public void setAge(int age) {
        if(age > 0) {
            this.age = age;
        } else {
            System.out.println("Please enter a valid age.");
        }
    }
}
  

In this example, we have private fields name and age. The getter and setter methods allow controlled access to these fields. Notice the setAge method validates the age before updating it.

Accessing Encapsulated Data

Once encapsulation is applied, we can access the private fields of a class through getter and setter methods. Hereโ€™s an example of how to create an object of the Person class and access the encapsulated data:

public class Main {
    public static void main(String[] args) {
        Person person = new Person();

        // Setting values using setter methods
        person.setName("John");
        person.setAge(25);

        // Getting values using getter methods
        System.out.println("Name: " + person.getName());
        System.out.println("Age: " + person.getAge());
    }
}
  

In this example, we create a Person object and use the setter methods to assign values to name and age. We then use the getter methods to retrieve these values and print them.

Advantages of Encapsulation

  • Improved Security: By keeping fields private and allowing access only through getters and setters, we ensure that the internal state is protected from unauthorized access.
  • Data Integrity: By adding validation inside setters (like checking if age is valid), we maintain the integrity of the data.
  • Code Reusability: The encapsulated class can be used in various parts of the application, promoting code reusability.
  • Flexibility to Change: If the implementation details change, it wonโ€™t affect the code that uses the class, making future modifications easier.

Encapsulation vs. Inheritance

While encapsulation focuses on hiding data and providing controlled access, inheritance allows one class to acquire the properties and methods of another class. Encapsulation deals with the internal workings of a class, whereas inheritance helps in building a relationship between classes.

Quick Tip:

Encapsulation helps in hiding the internal state of an object and provides a controlled interface through getter and setter methods. This ensures better data security, validation, and flexibility.


๐ŸŒŸ Enjoyed Learning with Us?

Help others discover Technorank Learning by sharing your honest experience.
Your support inspires us to keep building!

Leave a Google Review