Let’s talk about something every developer needs to understand, and every introvert secretly relates to: encapsulation.
In Java, encapsulation is all about boundaries. It’s how we protect the inner workings of our objects from the outside world, like giving your variables some well-deserved privacy after a long day of carrying your logic.
To make it less abstract (and more entertaining), my tutor decided to model this concept using... fighters. Yes. Think UFC. But for code.
What is Encapsulation Anyway?
Encapsulation is one of the pillars of Object-Oriented Programming (OOP), and it basically means: keep your data safe, and only allow access to it in controlled ways.
Java enforces this by:
- Making class attributes private
- Giving you controlled access via getters and setters
In other words, don’t let random code mess with your objects. You wouldn’t let strangers walk into your house and change your thermostat, so why let random classes tweak your fighter’s weight class?
Building a Fighter Class (With Boundaries, of Course)
Here’s the start of my Fighter.java class, where each fighter has a name, nationality, category, record, and more:
public class Fighter {
private String name;
private String nationality;
private int age;
private float height;
private float weight;
private String category;
private int victories;
private int defeats;
private int draws;
public void setName(String n) {
this.name = n;
}
public String getName() {
return this.name;
}
// And so on for the other attributes...
}
All of the attributes are private. Why? Because I don’t want anyone just casually changing a fighter’s weight mid-fight. That’s how bugs (and lawsuits) happen.
If someone needs to update a fighter’s data, they have to go through proper channels: getters and setters. Think of it like having to go through a coach before asking the fighter anything. That’s just respect.
But Wait... There’s More: Controlled Logic
Setters aren’t just doors. They’re smart doors. You can add logic to them!
public void setWeight(float w) {
this.weight = w;
this.setCategory();
}
private void setCategory() {
if (this.weight < 52.2) {
this.category = "Invalid";
} else if (this.weight <= 70.3) {
this.category = "Lightweight";
} else if (this.weight <= 83.9) {
this.category = "Middleweight";
} else if (this.weight <= 120.2) {
this.category = "Heavyweight";
} else {
this.category = "Invalid";
}
}
Here, when someone sets the weight, the category is automatically updated. Encapsulation + automation = clean, maintainable code.
No manual adjustments. No surprises. Just logic.
Why It Matters
Encapsulation isn’t just about keeping your code neat. It’s about:
- Preventing invalid data (no fighters with negative victories, please)
- Maintaining control over object state
- Keeping your sanity when debugging
You don’t want rogue classes running around setting your draws to -2. Letting objects guard their own data is how we make software more robust, and less dramatic than a weigh-in gone wrong.
TL-DR (Too Logical-Didn’t Read)
- Make your attributes private
- Use getters to read data
- Use setters to write data (with logic, if needed)
- Encapsulation = code boundaries + data safety
And if your fighters are losing too many matches, maybe don’t blame Java. Blame your matchmaking.
Top comments (0)