Getter and Setter in Java

1. Introduction

In Java, getters and setters are fundamental components of the encapsulation principle, which is a core aspect of object-oriented programming (OOP). Encapsulation is the technique of bundling the data (attributes) and code (methods) that operate on the data into a single unit or class and restricting access to some of the object's components. 

Getters and setters provide read and write access to an object's private fields. A getter method allows reading a property's value, while a setter method allows modifying it. This approach safeguards data integrity by ensuring only designated methods can access or modify the data.

2. Program Steps

1. Define a class with private fields.

2. Implement getter methods to retrieve the values of the fields.

3. Implement setter methods to set the values of the fields.

4. Demonstrate the use of getters and setters by creating an object, setting its fields via setters, and reading its fields via getters.

3. Code Program

public class Person {
    // Step 1: Defining private fields
    private String name;
    private int age;

    // Step 2: Implementing getter for the 'name' field
    public String getName() {
        return name;
    }

    // Step 3: Implementing setter for the 'name' field
    public void setName(String name) {
        this.name = name;
    }

    // Implementing getter for the 'age' field
    public int getAge() {
        return age;
    }

    // Implementing setter for the 'age' field
    public void setAge(int age) {
        this.age = age;
    }
}

public class Main {
    public static void main(String[] args) {
        // Creating an instance of Person
        Person person = new Person();

        // Using setter methods to set values
        person.setName("Ramesh");
        person.setAge(30);

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

Output:

Name: Ramesh
Age: 30

Explanation:

1. The Person class defines two private fields: name (of type String) and age (of type int). Making these fields private restricts their direct access from outside the class, adhering to the encapsulation principle.

2. Getter methods (getName and getAge) are implemented to provide read access to the private fields. These methods return the current values of name and age, respectively.

3. Setter methods (setName and setAge) are implemented to allow the modification of the private fields. These methods accept a parameter and assign its value to the corresponding field. This controlled access ensures that the fields can be updated safely.

4. In the Main class, an instance of Person is created, and its fields are set using the setter methods. The values of these fields are then retrieved using the getter methods and printed to the console.

5. This example illustrates how getters and setters facilitate controlled access to an object's data. They allow external classes to interact with the object's fields in a safe manner, thereby protecting the integrity of the data.

Comments