Unit 5: Writing Classes
Accessor and Mutator Methods in Java Getters and Setters
Introduction to Accessors and Mutators
In the realm of Object-Oriented Programming (OOP), encapsulation is a pillar that promotes the bundling of data (attributes) and methods (functions) that operate on the data into a single unit called a class. To protect this data and maintain the integrity of objects, we introduce accessor (getter) and mutator (setter) methods.
Understanding Getters and Setters
Why Accessors and Mutators?
- Data Protection: By making class attributes private, direct modification from outside the class is restricted. Accessors and mutators provide controlled access.
- Validation: Mutators (setters) can validate new data before setting it.
- Flexibility: Changes in how data is stored or represented in the class won't affect external code that uses getters and setters.
Anatomy of Getters and Setters
- Accessor (Getter): Returns the value of a private attribute.
public class Circle {
private double radius;
// Accessor method for radius
public double getRadius() {
return radius;
}
}
- Mutator (Setter): Sets the value of a private attribute, with potential validation.
public class Circle {
private double radius;
// Mutator method for radius
public void setRadius(double r) {
if (r > 0) {
radius = r;
} else {
System.out.println("Invalid radius value.");
}
}
}
Note
Always consider whether a class attribute genuinely needs a setter. Not all attributes should be changeable from outside the class. Sometimes, a read-only (just a getter) approach is safer.
Best Practices with Getters and Setters
- Naming Convention: Stick to the get<AttributeName> for getters and set<AttributeName> for setters convention.
- Minimal Logic in Getters: Ideally, getters should just return the attribute's value without additional computations.
- Validation in Setters: Ensure data integrity by validating data before changing the attribute's value.
Avoid Overusing
While getters and setters offer many advantages, overusing them can lead to bloated code. Always question if direct access to an attribute is necessary. Aim for meaningful interactions with an object's state rather than just creating getters and setters for every attribute.
Summary
Accessor and mutator methods, or getters and setters, are crucial tools in a Java programmer's toolkit. They strike a balance between data protection and accessibility, embodying the essence of encapsulation in OOP. By crafting meaningful and efficient getters and setters, AP Computer Science A students ensure the robustness and flexibility of their Java applications.
References
AP CSA Homework Assignment
Assignment: Crafting Efficient Getters and Setters
Instructions
- Create a Person class with private attributes: name, age, and address. Implement appropriate getters and setters.
- For the age attribute, ensure the setter validates that the age is between 0 and 120.
- Implement a toString method in the Person class that provides a string representation of the object. Use the getters within this method.
- Bonus: Extend the Person class to have a BankAccount attribute (you can create a simple BankAccount class). Ensure encapsulation principles are maintained.
After creating the classes, write a small main program to test your implementations. Ensure that encapsulation is maintained, and getters and setters work as expected.