Law of Demeter — The Principle of Least Knowledge in Java OOP
Fast recall
Quick revision
Core answer
order.getCustomer().getAccount().getBalance(), where O reaches through its friend (order) to that friend's friend (customer) to that friend's friend's friend (account). Each dot in that chain is a coupling the caller shouldn't have. The fix is to push the knowledge down: ask order for what you need (order.getCustomerBalance()), so the caller doesn't know that a customer has an account that has a balance.Whether you understand coupling at the method-call level and can reason about why reaching through an object graph is a maintainability hazard. This is a less-commonly-asked question than SOLID, which makes it a good differentiator — knowing it signals you've read beyond the popular lists.
Reciting 'only talk to immediate friends' without showing the train-of-calls violation or explaining the underlying coupling problem. A second mistake: treating LoD as a hard rule — fluent APIs (Stream, StringBuilder) and DTOs deliberately violate it, and that's fine.
Explain the coupling math: each dot in a call train is a dependency on that object's shape, so N dots = N things that can break the caller when any of them changes. Mention that modern Java records and DTOs make LoD violations tempting, and that the principle applies to the 'tell, don't ask' style of OOP.
Interview-ready guide
Interview answer
The Law of Demeter, or principle of least knowledge, says a method should only talk to its immediate friends — itself, its parameters, the objects it creates, and its direct fields. Don't talk to strangers. The violation is the train wreck: order.getCustomer().getAccount().getBalance() — the caller reaches through three objects to get what it needs.
The reason this matters is coupling math. Each dot in that chain is a dependency on one class's shape. The caller now depends on Order, Customer, and Account — three classes whose API changes can break it, even though the caller never owned a Customer or an Account. The fix is to push the knowledge down: instead of the caller navigating the graph, Order exposes getCustomerBalance(), and the caller only talks to Order. Coupling drops from three classes to one, and Customer and Account can change freely.
This is the same coupling-reduction idea as depending on interfaces, but at the method-call level. It's also called 'tell, don't ask' — instead of asking an object for its data and acting on it, tell the object to do the thing itself.
The nuance is that LoD is a heuristic, not a rule. Fluent APIs like Stream and StringBuilder chain on purpose — each call returns the same type, so there are no strangers. DTOs and records are pure data — a chain on a parsed JSON object is the data's shape, not a coupling hazard. Blindly pushing every getB().getC() down creates a wrapper-explosion god class that forwards everything. The principle is sound — minimize the classes a caller depends on — but you apply the intent, not the dot count.
Law of Demeter — The Principle of Least Knowledge in Java OOP
Direct Answer
The Law of Demeter (LoD), or principle of least knowledge, says a method M of object O may only call methods on: O itself, M's parameters, any objects M created, O's direct components (fields), and a global object (used sparingly). In plain terms: don't talk to strangers — only talk to your immediate friends. The violation is a train of method calls like order.getCustomer().getAccount().getBalance(), where O reaches through its friend (order) to that friend's friend (customer) to that friend's friend's friend (account). Each dot in that chain is a coupling the caller shouldn't have. The fix is to push the knowledge down: ask order for what you need (order.getCustomerBalance()), so the caller doesn't know that a customer has an account that has a balance.
Common Mistakes & Pitfalls
Reciting 'only talk to immediate friends' without showing the train-of-calls violation or explaining the underlying coupling problem. A second mistake: treating LoD as a hard rule — fluent APIs (Stream, StringBuilder) and DTOs deliberately violate it, and that's fine.
Check your understanding
Answer these without looking back at the article.
- What is 'tell, don't ask' and how does it relate to the Law of Demeter?
- Does the Law of Demeter apply to Java records and DTOs?
- How does the Law of Demeter relate to the feature envy code smell?
- Can you apply the Law of Demeter in a fluent API like Java Streams without breaking the fluent style?
- What is the cost of blindly applying LoD, and how do you recognize when you've created a forwarding facade?
Related interview prep categories
1Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.