Introduction
This principle suggests that a class should not be forced to implement interfaces it does not use.
General
In simpler terms, it’s better to have small, focused interfaces that cater to specific needs rather than having a big interface with a lot of methods that might not be relevant to all implementing classes.
RestaurantEmployee.java
Waiter.java
The Waiter
class has methods (washDishes()
and cookFood()
) that are irrelevant to its role. This situation violates the Interface Segregation Principle because a class should not be forced to implement methods it doesn’t need or use.
To adhere to the Interface Segregation Principle, you should split the RestaurantEmployee
interface into smaller, more focused interfaces that cater to specific roles:
Now, you can have classes implement only the interfaces relevant to their roles: