Skip to main content
InterviewHubPRO
Section Overview
Q9

Question 9 of 50

Part 1: Core SOLID Principles & Fundamentals (1 - 10)

18%
SOLID & OOP Principles (50 Qs)•Part 1: Core SOLID Principles & Fundamentals (1 - 10)

What is the Law of Demeter (Principle of Least Knowledge) and how do you spot train-wreck code?

senior Levelsolid-principlesCore SOLID Principles

In Simple Words: A method should only call methods on:

  1. Itself (this).
  2. Its parameters.
  3. Objects it instantiated directly.
  4. Its direct member fields.

Train-Wreck Violation:

order.getCustomer().getAddress().getCity().getZipCode() exposes the internal structure of 4 classes! Refactor to order.getShippingZipCode().