Post

πŸ‘¨β€πŸ’» Comparison of Using Getter and Setter Methods in Object-Oriented Programming

A post to document insights about Setters, inspired by Object-Oriented Programming indie game development experiences.

πŸ‘¨β€πŸ’» Comparison of Using Getter and Setter Methods in Object-Oriented Programming

In object-oriented programming, Getter and Setter methods are crucial tools for controlling data access within objects, enhancing code safety and integrity. This article compares scenarios of using and not using Getter and Setter methods.

1️⃣ Case Without Using Getter and Setter

The example below declares the fields of the Person class as public, allowing direct access from outside the class.

1
2
3
4
public class Person {
    public String name;
    public int age;
}

In this case, external code can directly access and modify fields in the Person object.

1
2
3
4
5
6
7
8
9
10
public class Main {
    public static void main(String[] args) {
        Person person = new Person();
        person.name = "Alice";
        person.age = -5; // Possible to set an invalid negative age

        System.out.println("Name: " + person.name);
        System.out.println("Age: " + person.age);
    }
}

⚠️ Issues

  • There is no control mechanism to prevent setting a negative value for the age field.
  • As fields are declared public, the internal state of the object can be modified without restriction.

2️⃣ Case With Getter and Setter

In this approach, fields in the Person class are declared private, and access is provided through Getter and Setter methods.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
public class Person {
    private String name;
    private int age;

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

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

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

    // Setter for age
    public void setAge(int age) {
        if (age >= 0) {
            this.age = age;
        } else {
            System.out.println("Invalid age: " + age);
        }
    }
}

Now, access to the Person object’s fields from external code requires using the Getter and Setter methods.

1
2
3
4
5
6
7
8
9
10
public class Main {
    public static void main(String[] args) {
        Person person = new Person();
        person.setName("Alice");
        person.setAge(-5); // Attempt to set an invalid age

        System.out.println("Name: " + person.getName());
        System.out.println("Age: " + person.getAge());
    }
}

πŸ“ Output

1
2
3
Invalid age: -5
Name: Alice
Age: 0

🌟 Benefits

  • The setAge method validates the age to prevent setting a negative value.
  • Declaring fields as private prevents external modifications to the object’s internal state.

Using Getter and Setter methods provides the following advantages:

  • Maintains object integrity: Prevents invalid data from being assigned to object properties.
  • Validates data: Setter methods allow data validation before setting values.
  • Enhances code maintainability: Encapsulation of data access ensures consistent handling of data, even in complex code.

In conclusion, Getter and Setter methods effectively prevent unrestricted external access, boosting code reliability and maintainability.

Whenever external classes need to handle the object (which might be almost always), using a Setter is essential.


πŸŽ…πŸ» Support

🍻 Support link for indie game developers

For more development tips and insights, follow my social channels!

🌲Link tree

If you’d like to know more about me! πŸ•΅οΈπŸ» About Redping

This post is licensed under CC BY 4.0 by the author.