Are you taking CLEP classes? Try clep.ai
| preferred AP College board partner for AP classes
medium Solved by 3 students

Private Member Access Within a Class

< Prev
Next >

Consider the following BankAccount class definition.

public class BankAccount {
    private double balance;
    private String accountNumber;
    
    public BankAccount(String num, double bal) {
        accountNumber = num;
        balance = bal;
    }
    
    public void transfer(double amount, BankAccount other) {
        if (amount > 0 && amount <= this.balance) {
            this.balance -= amount;
            other.balance += amount;
        }
    }
    
    public double getBalance() {
        return balance;
    }
}

Which statement best explains why the transfer method can directly access the private instance variable other.balance?

A

Methods can access private fields of any object of the same class type passed as a parameter

B

The balance field is automatically made public when passed as a parameter

C

Private fields become accessible to all methods when objects are passed by reference

D

The transfer method has special privileges because it modifies multiple objects

Hint
Did You Know?
Explain Why
Explain All Answers
Check Answer
Show Correct Answer
Report Question
Mark for review

Question Leaderboard

Rank
User
Correct Count
Attempt Count
Total Time
Score
#1kaisuki12 0m 00s 90
#2y.seong202701 3m 02s -192
#3bommasam00019 28m 05s -1,665
Items per page:
10
1 – 3 of 3
No comments yet. Be the first to comment!

AI Tutor

How can I help?

APFIVE © 2020.
Email: apfive@apfive.org|Privacy Policy|FAQ