Explain the Interface Segregation Principle with a Worker interface example.
No class should be forced to implement methods it does not use. Split one big interface into small, focused ones so each class picks only what applies.
Explain it simply
A restaurant menu card for a tea stall should not list biryani, sushi and pizza. A customer who wants chai has to read through items the stall cannot serve. A big interface does the same to classes: they must write empty or failing methods for things they never do.
Example
// Fat interface
interface Worker {
void work();
void eat();
void sleep();
}
class Robot implements Worker {
public void work() { }
public void eat() { throw new UnsupportedOperationException(); } // forced
public void sleep() { throw new UnsupportedOperationException(); } // forced
}
// Segregated
interface Workable { void work(); }
interface Eatable { void eat(); }
class Human implements Workable, Eatable {
public void work() { }
public void eat() { }
}
class Robot2 implements Workable {
public void work() { }
}Code that only needs workers asks for Workable and works with both humans and robots.
What to say in the interview
"ISP says clients should not depend on methods they do not use. I break large interfaces into small role-based ones, so implementers provide only relevant behaviour and callers depend on the narrowest interface they need."
Common mistakes
- Making one interface per method regardless of how they are used together.