CS FundamentalsHardProduct-company design interview

Code contains `order.getCustomer().getAddress().getCity().toUpperCase()`. Which principle does it most directly violate, and why does it matter?

  1. A. Single Responsibility; the Order class does too much
  2. B. Law of Demeter; the caller depends on the internal structure of three other classes, so a change to any of them breaks it
  3. C. Liskov Substitution; the chain may return a subtype
  4. D. Interface Segregation; Customer has too many getters

Correct answer: B. Law of Demeter; the caller depends on the internal structure of three other classes, so a change to any of them breaks it

Demeter ('talk only to your immediate friends') warns against reaching through objects. The fix is to ask the nearer object for what you need, e.g. order.shippingCity(), which reduces coupling.

More Software Engineering, Git and Cloud Basics questions

Exams that ask this topic