Skip to content
Sharp

Object-oriented programming · interview question

Consider the following code: What is a flaw in using public getters and setters for the 'balance' field in a BankAccount class?

Multiple choiceHardchapter-3-curatedpremiumoopgetters-and-setters-design
java
public class BankAccount { private double balance; public double getBalance() { return balance; } public void setBalance(double balance) { this.balance = balance; } }
Choose one answer
Answer and explanation

Correct answer

A. It allows unauthorized modification to the balance.

Why

Public getters and setters for the 'balance' field allow direct access and modification, bypassing any necessary validation or business logic that might ensure the account balance remains consistent and secure.

Test yourself on Object-oriented programming

A free test with instant, explained results. No account needed.

Start free test
All Object-oriented programming questions →