-1

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?

Soutzikevich
  • 963
  • 3
  • 13
  • 29
parlad
  • 1,096
  • 4
  • 21
  • 39
  • 2
    *Which* Herberd Schildt book? What is the context in which this figure appears? Does the context explain what "protect" means? – RealSkeptic May 01 '19 at 08:48
  • That's why there is "Head First Java". – mudit_sen May 01 '19 at 08:49
  • @RealSkeptic, the context is encapsulation and it does not explain what protected means. – parlad May 01 '19 at 08:53
  • 1
    It's a little guessing what exactly the author means, but an explanation can be the following: all access to the variable goes through the method, which can, for example, validate the input, so the input is protected from becoming stale. Example: `void setCertainProperty(int value) { if (value > 0) { this.certainProperty = value; } else { /* do nothing or throw exception */ } }`. This example shows that the value of `certainProperty` cannot be zero or below. – MC Emperor May 01 '19 at 08:57
  • I assume he means that by exposing a **method**, you can keep other data private. For example, you could have a field `private BigInteger MyBankBalance;` You can create a public accessor method (getter) to retrieve that information, without exposing the actual variable. While potentially anyone could access that variable's value and do whatever they want with it, nobody would be able to alter the actual variable. *Remember that Java is "pass by value".* – Soutzikevich May 01 '19 at 09:04
  • @RealSkeptic The book is very concise and understandable. I don't know what confused the OP. – Soutzikevich May 01 '19 at 09:21
  • With a public set method you could control what values area allowed to be set for instance, like only positive numbers for a number or not allowing the variable to change if the object is in a certain state. – Joakim Danielson May 01 '19 at 10:06
  • No, I don't think this is about data privacy - I think that is the source of the confusion here. By the word "protect", the author means that an attribute cannot be modified by a class other than the one that owns it. The "public method" helps because it is an alternative to a public attribute, which could be written by any code outside of the class in question. In other words, the method is part of making that attribute read-only. – halfer May 01 '19 at 15:37

1 Answers1

1

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?

Soutzikevich
  • 963
  • 3
  • 13
  • 29
  • public methods can be used to expose private data. sound different, so this saying is limiting the function/interface signature would limit resource access? – parlad May 01 '19 at 09:56
  • Correct. That is what it is saying. – Stephen C May 01 '19 at 11:44
  • @parladneupane I can see that you're confused. To give you the simplest example, see [this answer](https://stackoverflow.com/a/19436140/8075923). It explains what an accessor method is (a getter method). In my own, simple words, an accessor method, could be a public method that allows accessing any variable **of the same** class. If you have a variable that should be "shielded" by any external access, then that variable will be **private**. Now, you might have another class, that has to see the value for that private variable. Since it can't access it directly, it will use the accessor method. – Soutzikevich May 01 '19 at 15:12
  • @parladneupane **REMEMBER java is pass-by-value!!!!!!** See the updated answer. – Soutzikevich May 01 '19 at 15:30