Java : Accessor methods vs protected fields

2019-08-15 05:37发布

问题:

I know lots of coders use accessor methods to access some class fields which are private from other classes, but I was wondering why. And why they don't prefer protected fields witch are accessible only from classes of the same package instead of accessors? I mean if there is not a serious reason, it's just code waste.

回答1:

When you only define methods to access a field, you are restricted by the methods. You cannot do something that there is not a method for.

Consider this class:

public class Account {
    private int balance = 0;

    public int getBalance() {
        return balance;
    }

    public void insert(int amount) {
        if(amount > 0) {
            balance += amount;
        }
    }

    public void withdraw(int amount) {
        if(amount > 0 && amount =< balance) {
            balance -= amount;
        }
    }
}

You can change the balance of the account by inserting and withdrawing, and you can check what it is. But if you had access to the balance directly, you could do something that is not supposed to be possible like:

Account acc = new Account();
acc.balance = -10;

Furthermore, protected is actually closer to public than to private. If you have a private field, it will be private forever. If your field is protected, anyone can always extend your class and access the field. If it is intended to be private and you set it to protected, it might lose its purpose when someone extends it (and the fact that he extended no longer makes sense, because his new class does not behave in the spirit of the superclass).



回答2:

A mutuator method like a getter or a setter is not the same thing as a protected variable.

You have no control on when a protected variable is read or written if who is accessing it has the right to access it but a mutuator works as a bridge which is able to intercept modifications or access to an underlying member attribute and also provide different behavior from just returning/setting the value. So they don't fulfill exactly the same purpose.

In addition with mutuators you are able to provide a read-only or write-only access to a private member variable, but you can't do it with a protected field.



回答3:

Using accessors/mutators methods is a common best practice in Java programming as in other languages.

Wikipedia suggests:

The mutator method is most often used in object-oriented programming, in keeping with the principle of encapsulation. According to this principle, member variables of a class are made private to hide and protect them from other code, and can only be modified by a public member function (the mutator method), which takes the desired new value as a parameter, optionally validates it, and modifies the private member variable.

So you use accessors to hide the logic (if present) applied before setting or getting the private variable value.

protected modifier instead should be used to mark variables (or methods) that are not inteded to be publicly accessible, but that should be inherited and visible by sub classes. The sub class can use this variable in its methods and/or it can expose it publicly via accessors if necessary.