In the book Java The Complete Reference, Eleventh Edition by Herbert Schildt, in the chapter about encapsulation, there is a caption that says:
public methods can be used to protect private data.
How would a public method protect some private data?
In the book Java The Complete Reference, Eleventh Edition by Herbert Schildt, in the chapter about encapsulation, there is a caption that says:
public methods can be used to protect private data.
How would a public method protect some private data?
Directly from the book:
The private methods and data can only be accessed by code that is a member of the class. Therefore, any other code that is not a member of the class cannot access a private method or variable. Since the private members of a class may only be accessed by other parts of your program through the class’ public methods, you can ensure that no improper actions take place. Of course, this means that the public interface should be carefully designed not to expose too much of the inner workings of a class.
Like I said in a comment above, you might have written an API that is probably going to be used by the public. For some reason, you don't want to expose the inner working of your class (this is usually done by having an interface specify only the public methods you will expose and make your class implement that interface. The user of your API would create an instance using your interface, therefore having access only to the publicly available methods that you specified. It is not as straightforward as I make it seem, it includes using a name registry service like Java RMI Registry Naming Service, but I just want you to get the main idea).
What you would do, is as simple as Herbert Schildt describes. Everything in your class will actually be private, apart from the methods that you want to expose; protecting any sensitive data.
See below a simple example of two different Java classes, that have to work together:
public class BankAccount{
/*This is my money (for monetary amounts BigInteger is used, but this is an example)
Of course, I don't want anyone to have direct access to this value!
It's my money after all! What happens when the BankManager needs to check
how much money I have, to approve me for a loan?*/
private int balance = 900;
/*This is where an accessor method comes into play! While BankManager
is unable to access a private field, like my balance, he will be
able to access a PUBLIC METHOD, which will return the value of my balance!
Remember, Java is pass-by-value, so I will return just the value '900', not
the actual pointer to the variable in memory*/
public int getBalance(){
return this.balance;
}
}
public class BankManager{
/*Let's assume that BankAccount instance has already been created
named as "pedrosAccount"*/
//Then we can do
public int pedrosBalance = pedrosAccount.getBalance();
}
As you can see, from the above example, the bank manager can now create a local variable that will hold my money. He can do whatever operation he needs to do with that variable and that will not change the one in BankAccount. If the manager could edit the variable with my money, then that means that if he wanted he could steal all of it.
Do you see now, how through a public method, we have protected all my money from the (probably greedy) bank manager?