Reputation:
I am a beginner with respect to programming in OOPS. I was going through the concept of access modifiers in a book and got stuck at a place:
The code is as follows(I didn't care about the syntax of the code as the doubt is a conceptual one):
public class Soldier{
private int health;
public int getHealth(){
return health;
}
public void setHealth(int newHealth){
health = newHealth;
}
}
class Hospital{
private void healSoldier(Soldier soldierToHeal){
int health = soldierToHeal.getHealth();
health = health + 10;
soldierToHeal.setHealth(health);
}
public static void main(){
Soldier mySoldier = new Soldier();
mySoldier.setHealth(100);
Hospital militaryHospital = new Hospital();
mySoldier.setHealth(10); //Soldier wounded
militaryHospital.healSoldier(mySoldier);//Soldier's health increased by 10
}
}
I had a doubt in the healSoldier(Soldier soldierToHeal)
method. Since this method is private, it can be accessed only within the Hospital
class according to what I understood regarding the private
access modifier. But we are using the same method in main
to heal the soldier. Is it possible for an object of a class to have an access to its private method from main
?
Thanks in advance!!
Upvotes: 2
Views: 2748
Reputation: 507
"Since this method is private, it can be accessed only within the Hospital class according to what I understood regarding the private access modifier."
This is a wrong understanding.
The right understanding is: "..., the member or constructor is declared private, and access is permitted if and only if it occurs within the body of the top-level type that encloses the declaration of the member or constructor." see https://docs.oracle.com/javase/specs/jls/se11/html/jls-6.html#jls-6.6.1 https://docs.oracle.com/javase/specs/jls/se17/html/jls-6.html#jls-6.6.1
Upvotes: 0
Reputation: 393831
The only reason this is allowed is that your main
method belongs to the body of the same class - Soldier
- that also contains the body of the Hospital
class. This allows it to access all private
members and methods of any instance of the Hospital
class.
That said, if objects of the Hospital
class are going to be used by other classes, and should be allowed to call healSoldier
, you should make healSoldier
public. And it makes little sense for the Hospital
class to be an inner class of the Soldier
class. It should be a top level class.
Upvotes: 3
Reputation: 131346
Is it possible for an object of a class to have an access to its private method from main?
If the object is manipulated inside a method of this class, yes.
In this case it is able as the main(String[] args)
method is a method of Hospital
:
class Hospital {
private void healSoldier(Soldier soldierToHeal) {
int health = soldierToHeal.getHealth();
health = health + 10;
soldierToHeal.setHealth(health);
}
public static void main(String[] args) {
Soldier mySoldier = new Soldier();
mySoldier.setHealth(100);
Hospital militaryHospital = new Hospital();
mySoldier.setHealth(10); // Soldier wounded
militaryHospital.healSoldier(mySoldier);// Soldier's health increased by 10
}
}
I didn't care about the syntax of the code as the doubt is a conceptual one)
The syntax matters.
You probably incorrectly entered the code of the class in your actual question as the main()
method seems to be defined outside of any class.
Upvotes: 1
Reputation: 187
private variables and methods can be accessed anywhere from the instance of the class that declares it.
In your case the healSoldier method is called via the object of class Hospital which has declared it.
Upvotes: 0