What is the Interface Segregation Principle?
Learn the Interface Segregation Principle (SOLID) — why fat interfaces hurt design, with a Java example splitting one into focused ones.
Expected Interview Answer
The Interface Segregation Principle (the 'I' in SOLID) states that no client should be forced to depend on methods it does not use, so large interfaces should be split into smaller, role-specific ones.
Instead of one fat interface covering every possible operation, ISP favors several narrow, cohesive interfaces so a class implements only what is relevant to its role. This avoids the situation where an implementer must provide dummy or exception-throwing bodies for methods that make no sense for it, which is a strong smell of a leaking abstraction. ISP works hand in hand with the Single Responsibility Principle, but applies that idea to contracts rather than classes. It also keeps compilation and recompilation cheap in statically typed languages, since a change to one narrow interface does not ripple through unrelated implementers.
- Prevents classes from implementing irrelevant methods
- Reduces coupling between unrelated capabilities
- Makes interfaces easier to understand and implement correctly
- Limits the blast radius of interface changes
AI Mentor Explanation
A club could issue one giant 'PlayerDuties’ certificate covering batting, bowling, wicketkeeping and captaincy, forcing every player to sign off on skills they will never use. Instead, clubs issue separate specialist certifications — a batting badge, a bowling badge, a keeping badge — and a player only earns the ones relevant to their role. That is interface segregation: instead of one bloated contract, you offer focused ones, and nobody is forced to claim competence in something they do not do.
Step-by-Step Explanation
Step 1
Spot the fat interface
Look for an interface where implementers regularly stub out or throw from methods they do not need.
Step 2
Group methods by client role
Identify which callers actually need which subset of methods.
Step 3
Split into focused interfaces
Define smaller interfaces such as Readable, Writable, Printable instead of one Machine interface.
Step 4
Implement only what applies
Each class implements the combination of narrow interfaces relevant to its role.
What Interviewer Expects
- A correct definition tied to "clients should not depend on unused methods"
- A concrete before/after example of splitting a fat interface
- Recognition of the smell: empty or exception-throwing method bodies
- Awareness that ISP works alongside SRP but at the contract level
Common Mistakes
- Confusing ISP with the Single Responsibility Principle for classes
- Saying ISP means "always have one method per interface" (over-splitting is not the goal)
- Not recognizing throw new UnsupportedOperationException() as an ISP violation
- Forgetting that ISP applies to abstract classes with many abstract methods too
Best Answer (HR Friendly)
“Interface Segregation means designing several small, focused interfaces instead of one giant one, so a class only has to implement the methods that are actually relevant to it. If I see a class throwing an exception from a method it inherited but does not support, that usually means the interface should be split. It keeps contracts clean and implementations honest.”
Code Example
// Violates ISP: every printer must implement scan and fax
interface MultiFunctionDevice {
void print(String doc);
void scan(String doc);
void fax(String doc);
}
class BasicPrinter implements MultiFunctionDevice {
public void print(String doc) { System.out.println("Printing: " + doc); }
public void scan(String doc) { throw new UnsupportedOperationException(); }
public void fax(String doc) { throw new UnsupportedOperationException(); }
}
// Segregated interfaces
interface Printer { void print(String doc); }
interface Scanner { void scan(String doc); }
interface Fax { void fax(String doc); }
class SimplePrinter implements Printer {
public void print(String doc) { System.out.println("Printing: " + doc); }
}
class OfficeAllInOne implements Printer, Scanner, Fax {
public void print(String doc) { System.out.println("Printing: " + doc); }
public void scan(String doc) { System.out.println("Scanning: " + doc); }
public void fax(String doc) { System.out.println("Faxing: " + doc); }
}Follow-up Questions
- How does ISP relate to the Single Responsibility Principle?
- What is a code smell that signals an ISP violation?
- How do default methods in Java interfaces affect ISP?
- Can ISP be applied to abstract classes, not just interfaces?
MCQ Practice
1. The Interface Segregation Principle mainly addresses?
ISP is specifically about not forcing clients to depend on interface methods they never use.
2. A common symptom of violating ISP is?
Stubbed-out or exception-throwing method implementations signal the interface is too broad for that client.
3. ISP is best applied by?
ISP is satisfied by decomposing broad interfaces into smaller, cohesive ones matched to client needs.
Flash Cards
Interface Segregation Principle in one line? — No client should be forced to depend on methods it does not use.
Common violation smell? — A method implementation that throws or does nothing because it is irrelevant to that class.
Fix pattern? — Split one fat interface into several small, role-specific interfaces.
Related principle? — Single Responsibility Principle, but applied to contracts instead of classes.