Interface Segregation Principle
Many client-specific interfaces over one general interface
Overview
The Interface Segregation Principle (ISP) states that clients should not be forced to depend on interfaces they don't use. Instead of one large interface, create multiple smaller, focused interfaces.
This principle helps create more cohesive and flexible designs by preventing "fat" interfaces.
Key Concepts
Many small interfaces better than one large interface
Clients should only know about methods they use
Prevents "fat" or "polluted" interfaces
Each interface has a single purpose
Classes can implement multiple focused interfaces
Code Example
1// ❌ BAD: Fat interface - forces unnecessary implementations
2interface Worker {
3 void work();
4 void eat();
5 void sleep();
6 void attendMeeting();
7 void writeCode();
8 void manageTeam();
9}
10
11class Developer implements Worker {
12 public void work() { System.out.println("Coding"); }
13 public void eat() { System.out.println("Eating"); }
14 public void sleep() { System.out.println("Sleeping"); }
15 public void attendMeeting() { System.out.println("In meeting"); }
16 public void writeCode() { System.out.println("Writing code"); }
17 public void manageTeam() { /* Developer doesn't manage! */ } // Forced to implement
18}
19
20class Robot implements Worker {
21 public void work() { System.out.println("Working"); }
22 public void eat() { /* Robots don't eat! */ } // Forced to implement
23 public void sleep() { /* Robots don't sleep! */ } // Forced to implement
24 public void attendMeeting() { }
25 public void writeCode() { }
26 public void manageTeam() { }
27}
28
29// ✅ GOOD: Segregated interfaces
30interface Workable {
31 void work();
32}
33
34interface Eatable {
35 void eat();
36}
37
38interface Sleepable {
39 void sleep();
40}
41
42interface Programmer {
43 void writeCode();
44}
45
46interface Manager {
47 void manageTeam();
48}
49
50class Developer implements Workable, Eatable, Sleepable, Programmer {
51 public void work() { System.out.println("Coding"); }
52 public void eat() { System.out.println("Eating lunch"); }
53 public void sleep() { System.out.println("Sleeping"); }
54 public void writeCode() { System.out.println("Writing Java code"); }
55}
56
57class TechLead implements Workable, Eatable, Sleepable, Programmer, Manager {
58 public void work() { System.out.println("Leading and coding"); }
59 public void eat() { System.out.println("Eating"); }
60 public void sleep() { System.out.println("Sleeping"); }
61 public void writeCode() { System.out.println("Code review"); }
62 public void manageTeam() { System.out.println("Managing team"); }
63}
64
65class Robot implements Workable {
66 public void work() { System.out.println("Working 24/7"); }
67 // Only implements what it needs!
68}Small focused interfaces allow classes to implement only what they need. Robot only needs Workable, not eating or sleeping.
Advantages
- ✓
Classes only depend on methods they use
- ✓
Easier to implement and maintain
- ✓
More flexible - mix and match interfaces
- ✓
Better code organization
- ✓
Easier to test and mock
Best Practices
- 1.
Keep interfaces small and focused
- 2.
One interface = one capability/role
- 3.
Split large interfaces when clients use different subsets
- 4.
Use interface composition (class implements multiple)
- 5.
Review interfaces when adding new methods
💡 Interview Tips
Show fat interface vs. segregated interfaces
Use Worker/Robot example
Explain why forcing implementations is bad
Connect to Single Responsibility (for interfaces)
Discuss how it improves flexibility