ProgrammingModerateProduct-company placement · GATE CS

Which call violates the Law of Demeter (talk only to your immediate collaborators) inside an OrderService method that receives an Order order?

  1. A. order.getTotal()
  2. B. order.getCustomer().getAddress().getCity()
  3. C. this.repository.save(order)
  4. D. order.shippingCity()

Correct answer: B. order.getCustomer().getAddress().getCity()

The chain reaches through Customer and Address, objects the service did not get directly, so it couples the service to their structure. Asking the order itself (order.shippingCity()) hides that structure.

More OOP with C++ and Java questions

Exams that ask this topic