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

void washDishes();
void serveCustomers();
void cookFood();

Waiter.java

class Waiter implements RestaurantEmployee{
	
	@Override
	public void washDishes(){
		//not my job
	}
	
	@Override
	public void serveCustomers(){
		//my job
	}
	
	@Override
	public void cookFood(){
		//not my job
	}
	
}

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:

interface Dishwasher {
    void washDishes();
}
 
interface Server {
    void serveCustomers();
}
 
interface Cook {
    void cookFood();
}
 

Now, you can have classes implement only the interfaces relevant to their roles:

class Waiter implements Server {
    @Override
    public void serveCustomers() {
        // Serve customers
    }
}
 
class CookChef implements Cook {
    @Override
    public void cookFood() {
        // Cook food
    }
}
 
class DishwasherEmployee implements Dishwasher {
    @Override
    public void washDishes() {
        // Wash dishes
    }
}