Skip to content
Sharp

Object-oriented programming · interview question

In the context of an e-commerce platform, why might you use a method like 'updateStock(int quantity)' instead of a generic setter for a 'stock' field?

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

Correct answer

A. To encapsulate business logic for stock updates.

Why

Using a method like 'updateStock(int quantity)' allows the implementation of specific business logic for stock management, such as validating the quantity or updating related fields, which a generic setter cannot handle.

Test yourself on Object-oriented programming

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

Start free test
All Object-oriented programming questions →