Anatomy of a Class

AP Computer Science A· difficulty 1/5

public class Account {
    private double balance;
    public double getBalance() { return balance; }
    public void deposit(double amt) {
        if (amt > 0) balance += amt;
    }
}
// ...

Which best describes why balance is private?

  • A

    To enforce encapsulation; outside code must use methods that validate access and modification

    check_circle
  • B

    Because private variables run faster

  • C

    So they cannot be initialized

  • D

    So static methods cannot use them

Explanation

Encapsulation hides internal state and exposes a controlled interface, allowing validation logic in the methods.

Want 10 more like this — adaptive to your weak spots?

Related questions