# Soldier class, with health points

Recently I completed the encapsulation lesson and I made a simple android app implementing encapsulation, I had difficulties understanding encapsulation.

My Soldier class:

public class Soldier {
//Start of the class.

private int solHealth;

/* Soldier class constructor with the code to set the soldier health to 100
* when ever we create a soldier object.*/

public Soldier(){

this.setSolHealth(100);
}

//setter method with the parameter to set the solHealth variable.

private void setSolHealth(int solHealth) {
this.solHealth = solHealth;
}

//getter method to get the solHealth value.

public int getSolHealth(){

return this.solHealth;
}

//Main method to heal the soldier by 25 every time this method is called.

public int heal(){

this.solHealth += 25;

//Log used for debugging.

Log.i("Info", "" + solHealth);

return solHealth;
}

//Main method to injure the soldier by 25 every time this method is called.

public int injure(){

this.solHealth -= 25;

//Log used for debugging.

Log.i("Info", "" + solHealth);

return solHealth;
}

//End of the class.
}


I created a soldier object in the MainActivity class and used it to access getSolHealth(), heal(), and injure() methods, and to manipulate solHealth values when the button is pressed.

Did I succeed implementing encapsulation correctly ?

• I'd rename solHealth and the getter and setter of it to just health as it's quite obvious you are referring to the soldier's health Jul 21 '17 at 16:25
• I'll keep that in mind, thank you very much. Jul 21 '17 at 16:48
• After re-reading my comment I want to clarify I didn't mean I'd rename getSolHealth to health but to getHealth (the same applies to the setter) Jul 21 '17 at 16:49
• @Mibac Comments are for seeking clarifications to the question. Please write all suggestions, even trivial ones, as answers. Jul 21 '17 at 18:12

## 2 Answers

The short answer is yes, you succeeded in implementing encapsulation 100% correctly.

Still, I have some smaller problems with your code:

## Useless method

I noticed that you have a setter method for the Variable solHealth, which you should rename in my opinion to health. The problem is that the setter is useless as the method has the private access modifier and this method should never be used by the class itself. The only thing that you need to change when deleting this method is that you would have to change the constructor to be like this:

public Soldier() {
solHealth = 100;
}


This would still follow the concept of encapsulation, as encapsulation is simply a process of wrapping code and data together into a single unit, that allows only minimal manipulation from outer classes.

## Comments

If you are commenting your code you can easily implement documentation comments with which you can create JavaDoc pages. This is the standard of how to document methods, classes, and global variables/constants.

Example

/**
* Set's the soldiers health to the specified value
*
* @param health
*/
public void setHealth(int health) {
// Code here
}

• Thank you very much for your great answer, i agree with you the setter is useless, i took unnecessary big turn to accomplish initializing the health. I have another question in my mind now, when to use the setter then ? Jul 21 '17 at 22:22
• You should use setters only if you want to allow changing the value of a variable to a not predefined value from outer classes. This is the reason, why a setter with the private access modifier is most of the time if not always useless. Jul 21 '17 at 23:45
• can you gave me an example, please? Jul 22 '17 at 4:22
• So let's say you have a class that represents a weapon and you want to have multiple ways of attacking. If you would create a method for each way of getting hurt by everyone of the weapons in the Soldier class, this class would soon be unreadable. What you would do instead is creating one setter method in your Solider class and then call this setter with different parameters from the Weapon class. Jul 22 '17 at 8:56
• Niiiiiiiice! that's so cool!, thank you very much for you help justfoe thank you thank you thank you ! Jul 22 '17 at 10:05

Encapsulation in Java is a mechanism of wrapping the data (variables, which in your case is "solHealth") and code acting on the data (methods which introduce the state change of the variables, which is "setSolHealth, getSolHealth") together as a single unit.

In encapsulation, the variables of a class are hidden from other classes, and can be accessed only through the methods of their current class.

In class Soldier, variable "solHealth" is hidden as it is declared private and it can only be accessed via its methods which are setters(setSolHealth) and getters(getSolHealth).

**How to achieve the encapsulation:**

1.  Declare the variables of a class as private, which you have declared, access modifier private because it should only get accessed by method within the class, should not get accessed outside the class directly.

private int solHealth;

2. Provide public setter and getter methods to modify and view the variables values.

private void setSolHealth(int solHealth) {
this.solHealth = solHealth;
}

public int getSolHealth(){

return this.solHealth;
}

public int heal(){

this.solHealth += 25;

//Log used for debugging.

Log.i("Info", "" + solHealth);

return solHealth;
}

public int injure(){

this.solHealth -= 25;

//Log used for debugging.

Log.i("Info", "" + solHealth);

return solHealth;
}


A class can have total control over what is stored in its fields.

The users of a class do not know how the class stores its data. A class can change the data type of a field and users of the class do not need to change any of their code.

You have covered the basic steps to encapsulate an object, yes it is achieved.

Note: You can make either use of "Constructor" or "setter method" to set the value for an object. The constructor also initializes the object with a given set of value. You have a default constructor which also get automatically generated by the compiler when it compiles the class.

You can also make use of the constructor to initialize the value for the variable. This is another set of the implementation for setting the initializing the variables
public Soldier(){

this.setSolHealth= 100;
}


If you will omit the default constructor, you can make use of setter method to set the value for an object.

• Thank you so much for the help, correct me if im wrong, so from what i understand the use of setter is to modify a private varible from outside the encapsulated class but its useless if i wanted to modify it inside the encapsulated class, right? Jul 22 '17 at 7:11