Skip to content
Sharp

Object-oriented programming · interview question

In a Java class representing a product, why might you choose to make the 'price' field private with no public setter?

Multiple choiceEasychapter-3-curatedpremiumoopgetters-and-setters-design
Choose one answer
Answer and explanation

Correct answer

D. To prevent unauthorized modifications.

Why

Making the 'price' field private with no public setter ensures that only controlled methods within the class can modify the price, maintaining data integrity and preventing unauthorized changes.

Test yourself on Object-oriented programming

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

Start free test
All Object-oriented programming questions →