What is Coupling and Cohesion in OOP?
Understand coupling and cohesion in OOP: why low coupling and high cohesion produce maintainable, testable, reusable object-oriented code.
Expected Interview Answer
Coupling measures how dependent one module or class is on another, while cohesion measures how strongly the responsibilities inside a single module belong together.
Good object-oriented design pushes toward low coupling and high cohesion: classes should depend on as few other classes as possible, ideally through abstractions rather than concrete implementations, and each class should have one clear, tightly related set of responsibilities. High coupling makes a codebase brittle because a change in one class ripples into every dependent class, while low cohesion means a class is doing too many unrelated things, making it hard to understand, test, and reuse. Techniques like dependency injection, interfaces, and the single responsibility principle are the standard tools for driving coupling down and cohesion up.
- Localized changes instead of ripple effects
- Classes are easier to test in isolation
- Higher reusability of well-defined components
- Clearer mental model of what each class does
AI Mentor Explanation
A team with low coupling has specialists who each execute their own role — the opening batter doesn’t need to know the death-over bowler’s plans — so swapping one player rarely disrupts another’s game. High cohesion is the wicketkeeper doing only wicketkeeping-related jobs — glovework, stumping, calling the field — rather than also being asked to manage the scoreboard. A well-run side keeps roles tightly focused (cohesion) and independent of each other (loose coupling), so injuries or substitutions don’t cascade through the whole XI.
Step-by-Step Explanation
Step 1
Identify responsibilities
List what each class actually does and check if those duties are tightly related.
Step 2
Measure dependencies
Count how many other concrete classes each class directly references or instantiates.
Step 3
Introduce abstractions
Depend on interfaces rather than concrete classes to loosen coupling.
Step 4
Split unrelated duties
Extract unrelated responsibilities into their own classes to raise cohesion.
What Interviewer Expects
- A clear definition distinguishing coupling from cohesion
- Mentioning low coupling / high cohesion as the design goal
- Concrete techniques: dependency injection, interfaces, single responsibility principle
- A real example of tightly coupled or low-cohesion code and how to fix it
Common Mistakes
- Mixing up which term refers to inter-class dependency versus intra-class focus
- Assuming more classes automatically means better design
- Not connecting the concept to SOLID principles
- Giving a textbook definition with no concrete refactoring example
Best Answer (HR Friendly)
“Coupling is how tangled up different parts of the code are with each other, and cohesion is how focused a single class is on one clear job. We aim for low coupling and high cohesion, meaning classes are self-contained and only loosely connected to the rest of the system, so a change in one place doesn’t break everything else.”
Code Example
// Tight coupling: OrderService depends on a concrete class
class OrderService {
private MySqlDatabase db = new MySqlDatabase();
void save(Order o) { db.insert(o); }
}
// Loose coupling: OrderService depends on an abstraction
interface Database { void insert(Order o); }
class OrderServiceV2 {
private final Database db;
OrderServiceV2(Database db) { this.db = db; }
void save(Order o) { db.insert(o); }
}Follow-up Questions
- How does dependency injection reduce coupling?
- How does the single responsibility principle relate to cohesion?
- What is the difference between tight coupling and afferent/efferent coupling metrics?
- Can code have low coupling but still low cohesion?
MCQ Practice
1. Which design goal is generally preferred?
Low coupling and high cohesion together produce maintainable, testable, reusable classes.
2. A class that handles logging, billing, and email in one place has?
Unrelated responsibilities bundled into one class is a textbook case of low cohesion.
3. Programming against an interface instead of a concrete class primarily reduces?
Depending on an abstraction rather than a concrete implementation lowers coupling between classes.
Flash Cards
What is coupling? — How dependent one class or module is on the internals of another.
What is cohesion? — How closely related the responsibilities within a single class are.
What is the design goal? — Low coupling and high cohesion, for maintainable, testable code.
Name one technique to reduce coupling. — Dependency injection through interfaces instead of concrete classes.