Skip to content
Sharp

Object-oriented programming · interview question

Why would you prefer a method like 'calculateTotalPrice()' in an Order class over a public getter for a 'totalPrice' field?

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

Correct answer

D. To ensure the total price is always up-to-date with the latest data.

Why

Using a method like 'calculateTotalPrice()' ensures that the total price reflects the most recent data, such as changes in item prices or quantities, which a static field might not automatically reflect.

Test yourself on Object-oriented programming

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

Start free test
All Object-oriented programming questions →