What access modifier should be used for the 'balance' variable in the BankAccount class to ensure it can only be accessed within the class itself? Fill in the blanks in the code to implement this correctly.
Computer scienceProgramming languagesJavaCode organizationObject-oriented programmingClasses and objectsAccess control
Access modifiers
Guarding the vault: A banking riddle
Report a typo
Fill in the gaps with the relevant elements
BankAccount {
double balance;
public BankAccount(double initialBalance) {
balance = initialBalance;
}
public void deposit(double amount) {
balance += amount;
}
public boolean withdraw(double amount) {
if (balance >= amount) {
balance -= amount;
return true;
}
return false;
}
}Create a free account to access the full topic
By continuing, you agree to the JetBrains Academy Terms of Service as well as Hyperskill Terms of Service and Privacy Policy.